```
-- Standard list functions
```

modulePreludeList(map, (++),filter,concat,head,last,tail,init,null,length, (!!),foldl,foldl1,scanl,scanl1,foldr,foldr1,scanr,scanr1,iterate,repeat,replicate,cycle,take,drop,splitAt,takeWhile,dropWhile,span,break,lines,words,unlines,unwords,reverse,and,or,any,all,elem,notElem,lookup,sum,product,maximum,minimum,concatMap,zip,zip3,zipWith,zipWith3,unzip,unzip3)whereimportqualifiedChar(isSpace)infixl9!!infixr5++infix4 `elem`, `notElem` -- Map and appendmap::(a->b)->[a]->[b]mapf[]=[]mapf(x:xs)=fx:mapfxs(++)::[a]->[a]->[a] []++ys=ys(x:xs)++ys=x:(xs++ys)filter::(a->Bool)->[a]->[a]filterp[]=[]filterp(x:xs)|px=x:filterpxs|otherwise=filterpxsconcat::[[a]]->[a]concatxss=foldr(++) []xss-- head and tail extract the first element and remaining elements, -- respectively, of a list, which must be non-empty. last and init -- are the dual functions working from the end of a finite list, -- rather than the beginning.head::[a]->ahead(x:_)=xhead[]=error"Prelude.head: empty list"last::[a]->alast[x]=xlast(_:xs)=lastxslast[]=error"Prelude.last: empty list"tail::[a]->[a]tail(_:xs)=xstail[]=error"Prelude.tail: empty list"init::[a]->[a]init[x]=[]init(x:xs)=x:initxsinit[]=error"Prelude.init: empty list"null::[a]->Boolnull[]=Truenull(_:_)=False-- length returns the length of a finite list as an Int.length::[a]->Intlength[]=0length(_:l)=1+lengthl-- List index (subscript) operator, 0-origin (!!)::[a]->Int->axs!!n|n<0=error"Prelude.!!: negative index" []!!_=error"Prelude.!!: index too large" (x:_)!!n|n==0=x(_:xs)!!n=xs!!(n-1) -- foldl, applied to a binary operator, a starting value (typically the -- left-identity of the operator), and a list, reduces the list using -- the binary operator, from left to right: -- foldl f z [x1, x2, ..., xn] == (...((z `f` x1) `f` x2) `f`...) `f` xn -- foldl1 is a variant that has no starting value argument, and thus must -- be applied to non-empty lists. scanl is similar to foldl, but returns -- a list of successive reduced values from the left: -- scanl f z [x1, x2, ...] == [z, z `f` x1, (z `f` x1) `f` x2, ...] -- Note that last (scanl f z xs) == foldl f z xs. -- scanl1 is similar, again without the starting element: -- scanl1 f [x1, x2, ...] == [x1, x1 `f` x2, ...]foldl::(a->b->a)->a->[b]->afoldlfz[]=zfoldlfz(x:xs)=foldlf(fzx)xsfoldl1::(a->a->a)->[a]->afoldl1f(x:xs)=foldlfxxsfoldl1_[]=error"Prelude.foldl1: empty list"scanl::(a->b->a)->a->[b]->[a]scanlfqxs=q:(casexsof[]->[]x:xs->scanlf(fqx)xs)scanl1::(a->a->a)->[a]->[a]scanl1f(x:xs)=scanlfxxsscanl1_[]=[] -- foldr, foldr1, scanr, and scanr1 are the right-to-left duals of the -- above functions.foldr::(a->b->b)->b->[a]->bfoldrfz[]=zfoldrfz(x:xs)=fx(foldrfzxs)foldr1::(a->a->a)->[a]->afoldr1f[x]=xfoldr1f(x:xs)=fx(foldr1fxs)foldr1_[]=error"Prelude.foldr1: empty list"scanr::(a->b->b)->b->[a]->[b]scanrfq0[]=[q0]scanrfq0(x:xs)=fxq:qswhereqs@(q:_)=scanrfq0xsscanr1::(a->a->a)->[a]->[a]scanr1f[]=[]scanr1f[x]=[x]scanr1f(x:xs)=fxq:qswhereqs@(q:_)=scanr1fxs-- iterate f x returns an infinite list of repeated applications of f to x: -- iterate f x == [x, f x, f (f x), ...]iterate::(a->a)->a->[a]iteratefx=x:iteratef(fx) -- repeat x is an infinite list, with x the value of every element.repeat::a->[a]repeatx=xswherexs=x:xs-- replicate n x is a list of length n with x the value of every elementreplicate::Int->a->[a]replicatenx=taken(repeatx) -- cycle ties a finite list into a circular one, or equivalently, -- the infinite repetition of the original list. It is the identity -- on infinite lists.cycle::[a]->[a]cycle[]=error"Prelude.cycle: empty list"cyclexs=xs'wherexs'=xs++xs'-- take n, applied to a list xs, returns the prefix of xs of length n, -- or xs itself if n > length xs. drop n xs returns the suffix of xs -- after the first n elements, or [] if n > length xs. splitAt n xs -- is equivalent to (take n xs, drop n xs).take::Int->[a]->[a]taken_|n<=0=[]take_[]=[]taken(x:xs)=x:take(n-1)xsdrop::Int->[a]->[a]dropnxs|n<=0=xsdrop_[]=[]dropn(_:xs)=drop(n-1)xssplitAt::Int->[a]->([a],[a])splitAtnxs=(takenxs,dropnxs) -- takeWhile, applied to a predicate p and a list xs, returns the longest -- prefix (possibly empty) of xs of elements that satisfy p. dropWhile p xs -- returns the remaining suffix. span p xs is equivalent to -- (takeWhile p xs, dropWhile p xs), while break p uses the negation of p.takeWhile::(a->Bool)->[a]->[a]takeWhilep[]=[]takeWhilep(x:xs)|px=x:takeWhilepxs|otherwise=[]dropWhile::(a->Bool)->[a]->[a]dropWhilep[]=[]dropWhilepxs@(x:xs')|px=dropWhilepxs'|otherwise=xsspan,break::(a->Bool)->[a]->([a],[a])spanp[]=([],[])spanpxs@(x:xs')|px=(x:ys,zs)|otherwise=([],xs)where(ys,zs)=spanpxs'breakp=span(not.p) -- lines breaks a string up into a list of strings at newline characters. -- The resulting strings do not contain newlines. Similary, words -- breaks a string up into a list of words, which were delimited by -- white space. unlines and unwords are the inverse operations. -- unlines joins lines with terminating newlines, and unwords joins -- words with separating spaces.lines::String->[String]lines""=[]liness=let(l,s')=break(=='\n')sinl:cases'of[]->[] (_:s'')->liness''words::String->[String]wordss=casedropWhileChar.isSpacesof""->[]s'->w:wordss''where(w,s'')=breakChar.isSpaces'unlines::[String]->Stringunlines=concatMap(++"\n")unwords::[String]->Stringunwords[]=""unwordsws=foldr1(\ws->w++' ':s)ws-- reverse xs returns the elements of xs in reverse order. xs must be finite.reverse::[a]->[a]reverse=foldl(flip(:)) [] -- and returns the conjunction of a Boolean list. For the result to be -- True, the list must be finite; False, however, results from a False -- value at a finite index of a finite or infinite list. or is the -- disjunctive dual of and.and,or::[Bool]->Booland=foldr(&&)Trueor=foldr(||)False-- Applied to a predicate and a list, any determines if any element -- of the list satisfies the predicate. Similarly, for all.any,all::(a->Bool)->[a]->Boolanyp=or.mappallp=and.mapp-- elem is the list membership predicate, usually written in infix form, -- e.g., x `elem` xs. notElem is the negation.elem,notElem::(Eqa)=>a->[a]->Boolelemx=any(==x)notElemx=all(/=x) -- lookup key assocs looks up a key in an association list.lookup::(Eqa)=>a->[(a,b)]->Maybeblookupkey[]=Nothinglookupkey((x,y):xys)|key==x=Justy|otherwise=lookupkeyxys-- sum and product compute the sum or product of a finite list of numbers.sum,product::(Numa)=>[a]->asum=foldl(+) 0product=foldl(*) 1 -- maximum and minimum return the maximum or minimum value from a list, -- which must be non-empty, finite, and of an ordered type.maximum,minimum::(Orda)=>[a]->amaximum[]=error"Prelude.maximum: empty list"maximumxs=foldl1maxxsminimum[]=error"Prelude.minimum: empty list"minimumxs=foldl1minxsconcatMap::(a->[b])->[a]->[b]concatMapf=concat.mapf-- zip takes two lists and returns a list of corresponding pairs. If one -- input list is short, excess elements of the longer list are discarded. -- zip3 takes three lists and returns a list of triples. Zips for larger -- tuples are in the List libraryzip::[a]->[b]->[(a,b)]zip=zipWith(,)zip3::[a]->[b]->[c]->[(a,b,c)]zip3=zipWith3(,,) -- The zipWith family generalises the zip family by zipping with the -- function given as the first argument, instead of a tupling function. -- For example, zipWith (+) is applied to two lists to produce the list -- of corresponding sums.zipWith::(a->b->c)->[a]->[b]->[c]zipWithz(a:as) (b:bs)=zab:zipWithzasbszipWith___=[]zipWith3::(a->b->c->d)->[a]->[b]->[c]->[d]zipWith3z(a:as) (b:bs) (c:cs)=zabc:zipWith3zasbscszipWith3____=[] -- unzip transforms a list of pairs into a pair of lists.unzip::[(a,b)]->([a],[b])unzip=foldr(\(a,b)~(as,bs)->(a:as,b:bs)) ([],[])unzip3::[(a,b,c)]->([a],[b],[c])unzip3=foldr(\(a,b,c)~(as,bs,cs)->(a:as,b:bs,c:cs)) ([],[],[])

Index

(HTML for this module was generated on Mar 20. About the conversion tool.)