StdList.dcl 7.9 KB
Newer Older
1 2
definition module StdList

Camil Staps's avatar
Camil Staps committed
3 4 5 6
/**
 * Basic operations on lists.
 */

7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
//	****************************************************************************************
//	Concurrent Clean Standard Library Module Version 2.2
//	Copyright 1998-2006 University of Nijmegen
//	****************************************************************************************

import StdClass
import StdInt,StdChar,StdReal

//	Instances of overloaded functions:

instance ==	[a] | == a
						special a=Int
								a=Char
								a=Real

instance <	[a] | Ord a
						special a=Int
								a=Char
								a=Real

instance length	[]
instance %		[a]

instance toString 	[x] | toChar x	 // Convert [x]    via [Char] into String
									special x=Char
instance fromString [x] | fromChar x // Convert String via [Char] into [x]
									special x=Char

//	List Operators:

Camil Staps's avatar
Camil Staps committed
37 38 39 40 41 42 43 44 45 46 47
//* The nth element of a list.
(!!) 	infixl 9	:: ![.a] !Int -> .a

//* Concatenate two lists.
(++)	infixr 5	:: ![.a] u:[.a] -> u:[.a]

//* Concatenate a list of lists.
flatten				:: ![[.a]] -> [.a]

//* True iff the list is empty.
isEmpty				:: ![.a] -> Bool
48 49 50

//	List breaking or permuting functions:

Camil Staps's avatar
Camil Staps committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
//* The head (first element) of a list.
hd			:: ![.a] -> .a

//* The tail (everything except the first element) of a list.
tl			:: !u:[.a] -> u:[.a]

//* The last element of a list.
last		:: ![.a] -> .a

//* Everything except the last element of a list.
init	 	:: ![.a] -> [.a]

/**
 * Take a number of elements from the start of a list. When there are not
 * enough elements, the original list is returned.
 */
take		:: !Int [.a] -> [.a]

//* Take elements from the start of a list as long as a predicate holds.
takeWhile	:: (a -> .Bool) !.[a] -> .[a]

/**
 * Drop a number of elements from the start of a list. When there are not
 * enough elements, `[]` is returned.
 */
drop		:: !Int !u:[.a] -> u:[.a]

//* Drop elements from the start of a list as long as a predicate holds.
dropWhile	:: (a -> .Bool) !u:[a] -> u:[a]

/**
 * @param A predicate `p`
 * @param A list `xs`
 * @result `{{takeWhile}} p xs`
 * @result `{{dropWhile}} p xs`
 */
span		:: (a -> .Bool) !u:[a] -> (.[a],u:[a])

/**
 * Filter a list using a predicate.
 * @result The list containing exactly those elements for which the predicate
 *   holds.
 */
filter		:: (a -> .Bool) !.[a] -> .[a]

//* Reverse a list.
reverse		:: ![.a] -> [.a]

/**
 * Insert an element when a certain predicate holds. When the predicate does
 * not hold for any element, the new element is added to the end of the list.
 * @param The predicate on the new element and the existing element
 * @param The new element
 * @param The list
 */
insert 		:: (a -> a -> .Bool) a !u:[a] -> u:[a]

/**
 * Insert an element at an index. When the index is out-of-range, add it to the
 * end of the list.
 */
insertAt	:: !Int .a u:[.a] -> u:[.a]

/**
 * Remove an element at a certain index from a list. When the index is
 * out-of-range, the original list is returned.
 */
removeAt	:: !Int !u:[.a] -> u:[.a]

/**
 * Replace an element at a certain index. When the index is out-of-range, the
 * original list is returned.
 */
updateAt 	:: !Int .a !u:[.a] -> u:[.a]

/**
 * @param An index `n` of the list
 * @param The list `xs`
 * @result `{{take}} n xs`
 * @result `{{drop}} n xs`
 */
132 133 134 135
splitAt		:: !Int u:[.a] -> ([.a],u:[.a])			//	(take n list,drop n list)

//	Creating lists:

Camil Staps's avatar
Camil Staps committed
136 137
//* Apply a function to every element of a list.
map			:: (.a -> .b) ![.a] -> [.b]
138

Camil Staps's avatar
Camil Staps committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
/**
 * Repeatedly apply a function.
 * @param The function `f`
 * @param The initial value `x`
 * @result `[x, f x, f (f x), f (f (f x)), ...]`
 */
iterate		:: (a -> a) a -> .[a]

/**
 * Create a list of indices for an existing list.
 * @param The list `xs`
 * @result `[0..{{length}} xs - 1]`
 */
indexList	:: !.[a] -> [Int]

//* Create a list of `n` elements `x`.
repeatn		:: !.Int a -> .[a]

//* Repeat an element indefinitely.
repeat		:: a -> [a]

//* Unzip a list of tuples to a tuple of lists.
unzip		::	![(.a,.b)] 		-> ([.a],[.b])
162

Camil Staps's avatar
Camil Staps committed
163 164 165 166 167 168
/**
 * Zip two lists to a list of tuples. The result list has the length of the
 * shortest of the two original lists.
 * Also see {{`zip`}}.
 */
zip2		:: ![.a] [.b] 		-> [(.a,.b)]
169

Camil Staps's avatar
Camil Staps committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
//* Same as {{`zip2`}}, but for two lists in a tuple.
zip			:: !(![.a],[.b]) 	-> [(.a,.b)]

//* Diagonally create the Cartesian product for two lists.
diag2		:: !.[a] .[b]		-> [.(a,b)]

//* Diagonally create the Cartesian product for three lists.
diag3		:: !.[a] .[b] .[c]	-> [.(a,b,c)]

//	Folding and scanning:

/**
 * Replace all list constructors with a left-associative operator.
 * For efficiency reasons, `foldl` is a macro, so that applications of this
 * functions are inlined.
 * Also see `foldr`.
 *
 * @param The operator (substitute for `Cons`)
 * @param The substitute for `Nil`
 * @param The list
 * @result `op (... (op (op (op r e0) e1) e2) ...) en`
 * @type (.a -> .(.b -> .a)) .a ![.b] -> .a
 */
193 194 195 196 197
foldl op r l :== foldl r l
	where
		foldl r []		= r
		foldl r [a:x]	= foldl (op r a) x

Camil Staps's avatar
Camil Staps committed
198 199 200 201 202 203 204 205 206 207 208 209
/**
 * Replace all list constructors with a right-associative operator.
 * For efficiency reasons, `foldr` is a macro, so that applications of this
 * functions are inlined.
 * Also see `foldl`.
 *
 * @param The operator (substitute for `Cons`)
 * @param The substitute for `Nil`
 * @param The list
 * @result `op e0 (op e1 (... (op en r) ...))`
 * @type (.a -> .(.b -> .b)) .b ![.a] -> .b
 */
210 211 212 213 214
foldr op r l :== foldr l
	where
		foldr []	= r
		foldr [a:x]	= op a (foldr x)

Camil Staps's avatar
Camil Staps committed
215 216 217 218 219 220 221 222 223 224
/**
 * `scan` is like {{`foldl`}} but returns a list of reduced values rather than
 * only the end result.
 *
 * @param An operator `op`
 * @param An initial value `r`
 * @param A list `xs = [e0, e1, e2, ...]`
 * @result `[r, op r e0, op (op r e0) e1, ...]`
 */
scan		::  (a -> .(.b -> a)) a ![.b] -> .[a]
225 226 227

//	On Booleans

Camil Staps's avatar
Camil Staps committed
228 229 230 231 232 233 234 235 236 237 238
//* Check that all booleans are {{`True`}}. Also see {{`all`}}.
and			:: ![.Bool] -> Bool

//* Check that at least one boolean is {{`True`}}. Also see {{`any`}}.
or			:: ![.Bool] -> Bool

//* Check that a predicate holds for at least one list element.
any			:: (.a -> .Bool) ![.a] -> Bool

//* Check that a predicate holds for all list elements.
all			:: (.a -> .Bool) ![.a] -> Bool
239 240 241

//	When equality is defined on list elements

Camil Staps's avatar
Camil Staps committed
242 243
//* Check if an element is in the list.
isMember		::    a	 !.[a]	-> Bool 	| Eq a
244 245 246
													special a=Int
															a=Char
															a=Real
Camil Staps's avatar
Camil Staps committed
247 248
//* Check that the intersection between two lists is not empty.
isAnyMember		:: !.[a]  !.[a] -> Bool 	| Eq a
249 250 251
													special a=Int
															a=Char
															a=Real
Camil Staps's avatar
Camil Staps committed
252 253
//* Remove the first occurrence of an element from the list.
removeMember	:: a !u:[a] -> u:[a] 		| Eq a
254 255 256
													special a=Int
															a=Char
															a=Real
Camil Staps's avatar
Camil Staps committed
257 258 259 260 261
/**
 * Remove the first occurrences of the elements of the second list from the
 * first list.
 */
removeMembers	:: !u:[a] !.[a]	-> u:[a] 	| Eq a
262 263 264
													special a=Int
															a=Char
															a=Real
Camil Staps's avatar
Camil Staps committed
265 266
//* Remove all duplicates from a list.
removeDup		:: !.[a] 		-> .[a] 	| Eq a
267 268 269
													special a=Int
															a=Char
															a=Real
Camil Staps's avatar
Camil Staps committed
270 271 272 273 274 275
/**
 * Remove the first occurrence of an element from a list.
 * @result The index of the removed element
 * @result The new list
 */
removeIndex 	:: !a !u:[a] -> (Int,u:[a])	| Eq a
276 277 278
													special a=Int
															a=Char
															a=Real
Camil Staps's avatar
Camil Staps committed
279 280 281 282 283
/**
 * Find the first element for which the next element is the same.
 * E.g., `limit [1,3,2,2,1,...] = 2`
 */
limit			:: !.[a] 		-> a 		| Eq a
284 285 286 287 288 289
													special a=Int
															a=Char
															a=Real

//	When addition is defined on list elements

Camil Staps's avatar
Camil Staps committed
290 291
//* Overloaded sum on a list. The sum of `[]` is {{`zero`}}.
sum :: !.[a] -> a |  + , zero  a
292 293 294 295 296
									special	a=Int
											a=Real

//	When multiplication and addition is defined on list elements

Camil Staps's avatar
Camil Staps committed
297 298
//* Overloaded product of a list. The product of `[]` is {{`one`}}.
prod :: !.[a] -> a | * , one  a
299 300
									special	a=Int
											a=Real
Camil Staps's avatar
Camil Staps committed
301 302
//* Overloaded average of a list. The average of `[]` gives a runtime error.
avg :: !.[a] -> a | / , IncDec a
303 304 305 306
									special	a=Int
											a=Real