3{-| A library for manipulating lists of values. Every value in a
4list must have the same type.
7@docs (::), (++), isEmpty, length, reverse, map
10@docs head, tail, last, filter, take, drop
12# Putting Lists Together
13@docs concat, concatMap, join, intersperse, zip, zipWith
19@docs foldr, foldl, foldr1, foldl1, scanl, scanl1
22@docs sum, product, maximum, minimum, all, any, and, or
25@docs sort, sortBy, sortWith
31{-| Add an element to the front of a list `(1 :: [2,3] == [1,2,3])` -}
33(::) = Native.List.cons
35{-| Puts two appendable things together:
37 [1,1] ++ [2,3] == [1,1,2,3]
38 "abc" ++ "123" == "abc123"
40(++) : appendable -> appendable -> appendable
41(++) = Native.List.append
46{-| Extract the first element of a list. List must be non-empty.
50head = Native.List.head
52{-| Extract the elements after the head of the list. List must be non-empty.
53`(tail [1,2,3] == [2,3])`
56tail = Native.List.tail
58{-| Extract the last element of a list. List must be non-empty.
62last = Native.List.last
64{-| Check if a list is empty `(isEmpty [] == True)` -}
71{-| Apply a function to every element of a list: `(map sqrt [1,4,9] == [1,2,3])` -}
72map : (a -> b) -> [a] -> [b]
75{-| Reduce a list from the left: `(foldl (::) [] [1,2,3] == [3,2,1])` -}
76foldl : (a -> b -> b) -> b -> [a] -> b
77foldl = Native.List.foldl
79{-| Reduce a list from the right: `(foldr (+) 0 [1,2,3] == 6)` -}
80foldr : (a -> b -> b) -> b -> [a] -> b
81foldr = Native.List.foldr
83{-| Reduce a list from the left without a base case. List must be non-empty. -}
84foldl1 : (a -> a -> a) -> [a] -> a
85foldl1 = Native.List.foldl1
87{-| Reduce a list from the right without a base case. List must be non-empty. -}
88foldr1 : (a -> a -> a) -> [a] -> a
89foldr1 = Native.List.foldr1
91{-| Reduce a list from the left, building up all of the intermediate results into a list.
93 scanl (+) 0 [1,2,3,4] == [0,1,3,6,10]
95scanl : (a -> b -> b) -> b -> [a] -> [b]
96scanl = Native.List.scanl
98{-| Same as scanl but it doesn't require a base case. List must be non-empty.
100 scanl1 (+) [1,2,3,4] == [1,3,6,10]
102scanl1 : (a -> a -> a) -> [a] -> [a]
103scanl1 = Native.List.scanl1
105{-| Keep only elements that satisfy the predicate:
106`(filter isEven [1..6] == [2,4,6])`
108filter : (a -> Bool) -> [a] -> [a]
109filter = Native.List.filter
111{-| Determine the length of a list: `(length [1,2,3] == 3)` -}
113length = Native.List.length
115{-| Reverse a list. `(reverse [1..4] == [4,3,2,1])` -}
117reverse = Native.List.reverse
119{-| Check to see if all elements satisfy the predicate. -}
120all : (a -> Bool) -> [a] -> Bool
123{-| Check to see if any elements satisfy the predicate. -}
124any : (a -> Bool) -> [a] -> Bool
127{-| Check to see if all elements are True. -}
131{-| Check to see if any elements are True. -}
135{-| Concatenate a list of appendable things:
137 concat [[1,2],[3],[4,5]] == [1,2,3,4,5]
138 concat ["tree","house"] == "treehouse"
140concat : [appendable] -> appendable
141concat = Native.List.concat
143{-| Map a given function onto a list and flatten the resulting lists.
145 concatMap f xs == concat (map f xs)
147concatMap : (a -> appendable) -> [a] -> appendable
148concatMap f list = concat (map f list)
150{-| Get the sum of the list elements. `(sum [1..4] == 10)` -}
151sum : [number] -> number
154{-| Get the product of the list elements. `(product [1..4] == 24)` -}
155product : [number] -> number
158{-| Find the maximum element in a non-empty list: `maximum [1,4,2] == 4` -}
159maximum : [comparable] -> comparable
162{-| Find the minimum element in a non-empty list: `minimum [3,2,1] == 1` -}
163minimum : [comparable] -> comparable
166{-| Split a list based on the predicate. -}
167partition : (a -> Bool) -> [a] -> ([a],[a])
171 x::xs -> let (bs,cs) = partition pred xs in
172 if pred x then (x::bs,cs) else (bs,x::cs)
174{-| Combine two lists, combining them into tuples pairwise.
175If one list is longer, the extra elements are dropped.
177 zip [1,2,3] [6,7] == [(1,6),(2,7)]
180zip : [a] -> [b] -> [(a,b)]
183{-| Combine two lists, combining them with the given function.
184If one list is longer, the extra elements are dropped.
186 zipWith (+) [1,2,3] [1,2,3,4] == [2,4,6]
188zipWith : (a -> b -> c) -> [a] -> [b] -> [c]
189zipWith = Native.List.zipWith
191{-| Decompose a list of tuples. -}
192unzip : [(a,b)] -> ([a],[b])
196 (x,y)::ps -> let (xs,ys) = (unzip ps) in (x::xs,y::ys)
198{-| Places the given value between all of the lists in the second
199argument and concatenates the result.
201 join "a" ["H","w","ii","n"] == "Hawaiian"
203join : appendable -> [appendable] -> appendable
204join = Native.List.join
206{-| Places the given value between all members of the given list.
208 intersperse "on" ["turtles","turtles","turtles"] == ["turtles","on","turtles","on","turtles"]
210intersperse : a -> [a] -> [a]
213 a::b::cs -> a :: sep :: intersperse sep (b::cs)
217{-| Take the first n members of a list: `(take 2 [1,2,3,4] == [1,2])` -}
218take : Int -> [a] -> [a]
219take = Native.List.take
221{-| Drop the first n members of a list: `(drop 2 [1,2,3,4] == [3,4])` -}
222drop : Int -> [a] -> [a]
223drop = Native.List.drop
225{-| Creates a list with *n* copies of a value:
226`(repeat 3 (0,0) == [(0,0),(0,0),(0,0)]`
228repeat : Int -> a -> [a]
229repeat = Native.List.repeat
231{-| Sort values from lowest to highest: `sort [3,1,5] == [1,3,5]`
233sort : [comparable] -> [comparable]
234sort = Native.List.sort
236{-| Sort values by a derived property.
239alice = { name="Alice", height=1.62 }
240bob = { name="Bob" , height=1.85 }
241chuck = { name="Chuck", height=1.76 }
243sortBy .name [chuck,alice,bob] == [alice,bob,chuck]
244sortBy .height [chuck,alice,bob] == [alice,chuck,bob]
246sortBy String.length ["mouse","cat"] == ["cat","mouse"]
249sortBy : (a -> comparable) -> [a] -> [a]
250sortBy = Native.List.sortBy
252{-| Sort values with a custom comparison function.
255sortWith flippedComparison [1..5] == [5,4,3,2,1]
257flippedComparison a b =
264This is also the most general sort function, allowing you
265to define any other: `sort == sortWith compare`
267sortWith : (a -> a -> Order) -> [a] -> [a]
268sortWith = Native.List.sortWith
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list