@@ -4,128 +4,163 @@ module List where
4import open Basics
4import open Basics
5import Native.List
5import Native.List
9+A library for manipulating lists of values. Every value in a list must have
13+@docs (::), (++), length, reverse, map
16+@docs head, tail, last, filter, take, drop
18+# Putting Lists Together
19+@docs concat, concatMap, join, intersperse, zip, zipWith
22+@docs split, partition, unzip
25+@docs foldr, foldl, foldr1, foldl1, scanl, scanl1
28+@docs susm, product, maximum, minimum, all, any, and, or
7−-- Add an element to the front of a list `(1 :: [2,3] == [1,2,3])`
32+{-| Add an element to the front of a list `(1 :: [2,3] == [1,2,3])` -}
8(::) : a -> [a] -> [a]
33(::) : a -> [a] -> [a]
9(::) = Native.List.cons
34(::) = Native.List.cons
11−-- Puts two appendable things together:
36+{-| Puts two appendable things together:
13−-- [1,1] ++ [2,3] == [1,1,2,3]
38+ [1,1] ++ [2,3] == [1,1,2,3]
14−-- "abc" ++ "123" == "abc123"
39+ "abc" ++ "123" == "abc123"
15(++) : appendable -> appendable -> appendable
41(++) : appendable -> appendable -> appendable
16(++) = Native.List.append
42(++) = Native.List.append
18−-- Extract the first element of a list. List must be non-empty.
44+{-| Extract the first element of a list. List must be non-empty.
19−-- `(head [1,2,3] == 1)`
45+`(head [1,2,3] == 1)`
20head : [a] -> a
47head : [a] -> a
21head = Native.List.head
48head = Native.List.head
23−-- Extract the elements after the head of the list. List must be non-empty.
50+{-| Extract the elements after the head of the list. List must be non-empty.
24−-- `(tail [1,2,3] == [2,3])`
51+`(tail [1,2,3] == [2,3])`
25tail : [a] -> [a]
53tail : [a] -> [a]
26tail = Native.List.tail
54tail = Native.List.tail
28−-- Extract the last element of a list. List must be non-empty.
56+{-| Extract the last element of a list. List must be non-empty.
29−-- `(last [1,2,3] == 3)`
57+`(last [1,2,3] == 3)`
30last : [a] -> a
59last : [a] -> a
31last = Native.List.last
60last = Native.List.last
33−-- Check if a list is empty `(isEmpty [] == True)`
62+{-| Check if a list is empty `(isEmpty [] == True)` -}
34isEmpty : [a] -> Bool
63isEmpty : [a] -> Bool
35isEmpty xs =
64isEmpty xs =
36 case xs of
65 case xs of
37 [] -> True
66 [] -> True
38 _ -> False
67 _ -> False
40−-- Apply a function to every element of a list: `(map sqrt [1,4,9] == [1,2,3])`
69+{-| Apply a function to every element of a list: `(map sqrt [1,4,9] == [1,2,3])` -}
41map : (a -> b) -> [a] -> [b]
70map : (a -> b) -> [a] -> [b]
42map = Native.List.map
71map = Native.List.map
44−-- Reduce a list from the left: `(foldl (::) [] "gateman" == "nametag")`
73+{-| Reduce a list from the left: `(foldl (::) [] "gateman" == "nametag")` -}
45foldl : (a -> b -> b) -> b -> [a] -> b
74foldl : (a -> b -> b) -> b -> [a] -> b
46foldl = Native.List.foldl
75foldl = Native.List.foldl
48−-- Reduce a list from the right: `(foldr (+) 0 [1,2,3] == 6)`
77+{-| Reduce a list from the right: `(foldr (+) 0 [1,2,3] == 6)` -}
49foldr : (a -> b -> b) -> b -> [a] -> b
78foldr : (a -> b -> b) -> b -> [a] -> b
50foldr = Native.List.foldr
79foldr = Native.List.foldr
52−-- Reduce a list from the left without a base case. List must be non-empty.
81+{-| Reduce a list from the left without a base case. List must be non-empty. -}
53foldl1 : (a -> a -> a) -> [a] -> a
82foldl1 : (a -> a -> a) -> [a] -> a
54foldl1 = Native.List.foldl1
83foldl1 = Native.List.foldl1
56−-- Reduce a list from the right without a base case. List must be non-empty.
85+{-| Reduce a list from the right without a base case. List must be non-empty. -}
57foldr1 : (a -> a -> a) -> [a] -> a
86foldr1 : (a -> a -> a) -> [a] -> a
58foldr1 = Native.List.foldr1
87foldr1 = Native.List.foldr1
60−-- Reduce a list from the left, building up all of the intermediate results into a list.
89+{-| Reduce a list from the left, building up all of the intermediate results into a list.
62−-- scanl (+) 0 [1,2,3,4] == [0,1,3,6,10]
91+ scanl (+) 0 [1,2,3,4] == [0,1,3,6,10]
63scanl : (a -> b -> b) -> b -> [a] -> [b]
93scanl : (a -> b -> b) -> b -> [a] -> [b]
64scanl = Native.List.scanl
94scanl = Native.List.scanl
66−-- Same as scanl but it doesn't require a base case. List must be non-empty.
96+{-| Same as scanl but it doesn't require a base case. List must be non-empty.
68−-- scanl1 (+) [1,2,3,4] == [1,3,6,10]
98+ scanl1 (+) [1,2,3,4] == [1,3,6,10]
69scanl1 : (a -> a -> a) -> [a] -> [a]
100scanl1 : (a -> a -> a) -> [a] -> [a]
70scanl1 = Native.List.scanl1
101scanl1 = Native.List.scanl1
72−-- Filter out elements which do not satisfy the predicate: `(filter isLower "AaBbCc" == "abc")`
103+{-| Keep only elements that satisfy the predicate:
104+`(filter isLower "AaBbCc" == "abc")`
73filter : (a -> Bool) -> [a] -> [a]
106filter : (a -> Bool) -> [a] -> [a]
74filter = Native.List.filter
107filter = Native.List.filter
76−-- Determine the length of a list: `(length "innumerable" == 11)`
109+{-| Determine the length of a list: `(length "innumerable" == 11)` -}
77length : [a] -> Int
110length : [a] -> Int
78length = Native.List.length
111length = Native.List.length
80−-- Reverse a list. `(reverse [1..4] == [4,3,2,1])`
113+{-| Reverse a list. `(reverse [1..4] == [4,3,2,1])` -}
81reverse : [a] -> [a]
114reverse : [a] -> [a]
82reverse = Native.List.reverse
115reverse = Native.List.reverse
84−-- Check to see if all elements satisfy the predicate.
117+{-| Check to see if all elements satisfy the predicate. -}
85all : (a -> Bool) -> [a] -> Bool
118all : (a -> Bool) -> [a] -> Bool
86all = Native.List.all
119all = Native.List.all
88−-- Check to see if any elements satisfy the predicate.
121+{-| Check to see if any elements satisfy the predicate. -}
89any : (a -> Bool) -> [a] -> Bool
122any : (a -> Bool) -> [a] -> Bool
90any = Native.List.any
123any = Native.List.any
92−-- Check to see if all elements are True.
125+{-| Check to see if all elements are True. -}
93and : [Bool] -> Bool
126and : [Bool] -> Bool
94and = foldl (&&) True
127and = foldl (&&) True
96−-- Check to see if any elements are True.
129+{-| Check to see if any elements are True. -}
97or : [Bool] -> Bool
130or : [Bool] -> Bool
98or = foldl (||) False
131or = foldl (||) False
100−-- Concatenate a list of appendable things:
133+{-| Concatenate a list of appendable things:
102−-- concat ["tree","house"] == "treehouse"
135+ concat ["tree","house"] == "treehouse"
103concat : [appendable] -> appendable
137concat : [appendable] -> appendable
104concat = Native.List.concat
138concat = Native.List.concat
106−-- Map a given function onto a list and flatten the resulting lists.
140+{-| Map a given function onto a list and flatten the resulting lists.
108−-- concatMap f xs == concat (map f xs)
142+ concatMap f xs == concat (map f xs)
109concatMap : (a -> appendable) -> [a] -> appendable
144concatMap : (a -> appendable) -> [a] -> appendable
110concatMap f list = concat (map f list)
145concatMap f list = concat (map f list)
112−-- Get the sum of the list elements. `(sum [1..4] == 10)`
147+{-| Get the sum of the list elements. `(sum [1..4] == 10)` -}
113sum : [number] -> number
148sum : [number] -> number
114sum = foldl (+) 0
149sum = foldl (+) 0
116−-- Get the product of the list elements. `(product [1..4] == 24)`
151+{-| Get the product of the list elements. `(product [1..4] == 24)` -}
117product : [number] -> number
152product : [number] -> number
118product = foldl (*) 1
153product = foldl (*) 1
120−-- Find the maximum element in a non-empty list: `maximum [1,4,2] == 4`
155+{-| Find the maximum element in a non-empty list: `maximum [1,4,2] == 4` -}
121maximum : [comparable] -> comparable
156maximum : [comparable] -> comparable
122maximum = foldl1 max
157maximum = foldl1 max
124−-- Find the minimum element in a non-empty list: `minimum [3,2,1] == 1`
159+{-| Find the minimum element in a non-empty list: `minimum [3,2,1] == 1` -}
125minimum : [comparable] -> comparable
160minimum : [comparable] -> comparable
126minimum = foldl1 min
161minimum = foldl1 min
128−-- Split a list based on the predicate.
163+{-| Split a list based on the predicate. -}
129partition : (a -> Bool) -> [a] -> ([a],[a])
164partition : (a -> Bool) -> [a] -> ([a],[a])
130partition pred lst =
165partition pred lst =
131 case lst of
166 case lst of
@@ -133,44 +168,49 @@ partition pred lst =
133 x::xs -> let (bs,cs) = partition pred xs in
168 x::xs -> let (bs,cs) = partition pred xs in
134 if pred x then (x::bs,cs) else (bs,x::cs)
169 if pred x then (x::bs,cs) else (bs,x::cs)
136−-- Combine two lists, combining them into tuples pairwise.
171+{-| Combine two lists, combining them into tuples pairwise.
137−-- If one list is longer, the extra elements are dropped.
172+If one list is longer, the extra elements are dropped.
139−-- zip [1,2,3] [6,7] == [(1,6),(2,7)]
174+ zip [1,2,3] [6,7] == [(1,6),(2,7)]
140−-- zip == zipWith (,)
175+ zip == zipWith (,)
141zip : [a] -> [b] -> [(a,b)]
177zip : [a] -> [b] -> [(a,b)]
142zip = Native.List.zip
178zip = Native.List.zip
144−-- Combine two lists, combining them with the given function.
180+{-| Combine two lists, combining them with the given function.
145−-- If one list is longer, the extra elements are dropped.
181+If one list is longer, the extra elements are dropped.
147−-- zipWith (+) [1,2,3] [1,2,3,4] == [2,4,6]
183+ zipWith (+) [1,2,3] [1,2,3,4] == [2,4,6]
148zipWith : (a -> b -> c) -> [a] -> [b] -> [c]
185zipWith : (a -> b -> c) -> [a] -> [b] -> [c]
149zipWith = Native.List.zipWith
186zipWith = Native.List.zipWith
151−-- Decompose a list of tuples.
188+{-| Decompose a list of tuples. -}
152unzip : [(a,b)] -> ([a],[b])
189unzip : [(a,b)] -> ([a],[b])
153unzip pairs =
190unzip pairs =
154 case pairs of
191 case pairs of
155 [] -> ([],[])
192 [] -> ([],[])
156 (x,y)::ps -> let (xs,ys) = (unzip ps) in (x::xs,y::ys)
193 (x,y)::ps -> let (xs,ys) = (unzip ps) in (x::xs,y::ys)
158−-- Split a list with a given seperator.
195+{-| Split a list with a given seperator.
160−-- split "," "hello,there,friend" == ["hello", "there", "friend"]
197+ split "," "hello,there,friend" == ["hello", "there", "friend"]
161split : [a] -> [a] -> [[a]]
199split : [a] -> [a] -> [[a]]
162split = Native.List.split
200split = Native.List.split
164−-- Places the given value between all of the lists in the second argument
202+{-| Places the given value between all of the lists in the second
165−-- and concatenates the result.
203+argument and concatenates the result.
167−-- join "a" ["H","w","ii","n"] == "Hawaiian"
205+ join "a" ["H","w","ii","n"] == "Hawaiian"
168join : appendable -> [appendable] -> appendable
207join : appendable -> [appendable] -> appendable
169join = Native.List.join
208join = Native.List.join
171−-- Places the given value between all members of the given list.
210+{-| Places the given value between all members of the given list.
173−-- intersperse ' ' "INCEPTION" == "I N C E P T I O N"
212+ intersperse ' ' "INCEPTION" == "I N C E P T I O N"
174intersperse : a -> [a] -> [a]
214intersperse : a -> [a] -> [a]
175intersperse sep xs =
215intersperse sep xs =
176 case xs of
216 case xs of
@@ -178,14 +218,16 @@ intersperse sep xs =
178 [a] -> [a]
218 [a] -> [a]
181−-- Take the first n members of a list: `(take 2 [1,2,3,4] == [1,2])`
221+{-| Take the first n members of a list: `(take 2 [1,2,3,4] == [1,2])` -}
182take : Int -> [a] -> [a]
222take : Int -> [a] -> [a]
183take = Native.List.take
223take = Native.List.take
185−-- Drop the first n members of a list: `(drop 2 [1,2,3,4] == [3,4])`
225+{-| Drop the first n members of a list: `(drop 2 [1,2,3,4] == [3,4])` -}
186drop : Int -> [a] -> [a]
226drop : Int -> [a] -> [a]
187drop = Native.List.drop
227drop = Native.List.drop
189−-- Creates a list with n copies of a value: `(replicate 3 (0,0) == [(0,0),(0,0),(0,0)]`
229+{-| Creates a list with *n* copies of a value:
230+`(repeat 3 (0,0) == [(0,0),(0,0),(0,0)]`
190repeat : Int -> a -> [a]
232repeat : Int -> a -> [a]
191repeat = Native.List.repeat
233repeat = Native.List.repeat