StdList.dcl 8.09 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 48 49 50 51 52 53 54 55
/**
 * The nth element of a list.
 */
(!!) 	infixl 9	:: ![.a] !Int -> .a

/**
 * Append 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
56 57 58

//	List breaking or permuting functions:

Camil Staps's avatar
Camil Staps committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
/**
 * 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

/**
75
 * Everything except the last element of a list.
Camil Staps's avatar
Camil Staps committed
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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
 */
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`
 */
154 155 156 157
splitAt		:: !Int u:[.a] -> ([.a],u:[.a])			//	(take n list,drop n list)

//	Creating lists:

Camil Staps's avatar
Camil Staps committed
158 159 160 161
/**
 * Apply a function to every element of a list.
 */
map			:: (.a -> .b) ![.a] -> [.b]
162

Camil Staps's avatar
Camil Staps committed
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
/**
 * 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])
192

Camil Staps's avatar
Camil Staps committed
193 194 195 196 197 198
/**
 * 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)]
199

Camil Staps's avatar
Camil Staps committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
/**
 * 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
 */
229 230 231 232 233
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
234 235 236 237 238 239 240 241 242 243 244 245
/**
 * 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
 */
246 247 248 249 250
foldr op r l :== foldr l
	where
		foldr []	= r
		foldr [a:x]	= op a (foldr x)

Camil Staps's avatar
Camil Staps committed
251 252 253 254 255 256 257 258 259 260
/**
 * `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]
261 262 263

//	On Booleans

Camil Staps's avatar
Camil Staps committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
/**
 * 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
283 284 285

//	When equality is defined on list elements

Camil Staps's avatar
Camil Staps committed
286 287 288 289
/**
 * Check if an element is in the list.
 */
isMember		::    a	 !.[a]	-> Bool 	| Eq a
290 291 292
													special a=Int
															a=Char
															a=Real
Camil Staps's avatar
Camil Staps committed
293 294 295 296
/**
 * Check that the intersection between two lists is not empty.
 */
isAnyMember		:: !.[a]  !.[a] -> Bool 	| Eq a
297 298 299
													special a=Int
															a=Char
															a=Real
Camil Staps's avatar
Camil Staps committed
300 301 302 303
/**
 * Remove the first occurrence of an element from the list.
 */
removeMember	:: a !u:[a] -> u:[a] 		| Eq a
304 305 306
													special a=Int
															a=Char
															a=Real
Camil Staps's avatar
Camil Staps committed
307
/**
308 309
 * Remove the first occurrences of the elements of the second list from the
 * first list.
Camil Staps's avatar
Camil Staps committed
310 311
 */
removeMembers	:: !u:[a] !.[a]	-> u:[a] 	| Eq a
312 313 314
													special a=Int
															a=Char
															a=Real
Camil Staps's avatar
Camil Staps committed
315 316 317 318
/**
 * Remove all duplicates from a list.
 */
removeDup		:: !.[a] 		-> .[a] 	| Eq a
319 320 321
													special a=Int
															a=Char
															a=Real
Camil Staps's avatar
Camil Staps committed
322 323 324 325 326 327
/**
 * 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
328 329 330
													special a=Int
															a=Char
															a=Real
Camil Staps's avatar
Camil Staps committed
331 332
/**
 * Find the first element for which the next element is the same.
Camil Staps's avatar
Camil Staps committed
333
 * E.g., `limit [1,3,2,2,1,...] = 2`
Camil Staps's avatar
Camil Staps committed
334 335
 */
limit			:: !.[a] 		-> a 		| Eq a
336 337 338 339 340 341
													special a=Int
															a=Char
															a=Real

//	When addition is defined on list elements

Camil Staps's avatar
Camil Staps committed
342 343 344 345
/**
 * Overloaded sum on a list. The sum of `[]` is {{`zero`}}.
 */
sum :: !.[a] -> a |  + , zero  a
346 347 348 349 350
									special	a=Int
											a=Real

//	When multiplication and addition is defined on list elements

Camil Staps's avatar
Camil Staps committed
351 352 353 354
/**
 * Overloaded product of a list. The product of `[]` is {{`one`}}.
 */
prod :: !.[a] -> a | * , one  a
355 356
									special	a=Int
											a=Real
Camil Staps's avatar
Camil Staps committed
357 358 359 360
/**
 * Overloaded average of a list. The average of `[]` gives a runtime error.
 */
avg :: !.[a] -> a | / , IncDec a
361 362 363 364
									special	a=Int
											a=Real