@@ -1,29 +1,34 @@
2module List where
2module List where
4−import Native.Utils (min, max)
4+import Native.Utils
5−import Native.List as L
5+import Native.List as Native
7-- Add an element to the front of a list `(1 :: [2,3] == [1,2,3])`
7-- Add an element to the front of a list `(1 :: [2,3] == [1,2,3])`
8(::) : a -> [a] -> [a]
8(::) : a -> [a] -> [a]
10-- Puts two appendable things together:
11-- Puts two appendable things together:
12-- [1,1] ++ [2,3] == [1,1,2,3]
13-- [1,1] ++ [2,3] == [1,1,2,3]
13-- "abc" ++ "123" == "abc123"
14-- "abc" ++ "123" == "abc123"
14−(++) : Appendable a -> Appendable a -> Appendable a
15+(++) : appendable -> appendable -> appendable
16-- Extract the first element of a list. List must be non-empty.
18-- Extract the first element of a list. List must be non-empty.
17-- `(head [1,2,3] == 1)`
19-- `(head [1,2,3] == 1)`
18head : [a] -> a
20head : [a] -> a
20-- Extract the elements after the head of the list. List must be non-empty.
23-- Extract the elements after the head of the list. List must be non-empty.
21-- `(tail [1,2,3] == [2,3])`
24-- `(tail [1,2,3] == [2,3])`
22tail : [a] -> [a]
25tail : [a] -> [a]
24-- Extract the last element of a list. List must be non-empty.
28-- Extract the last element of a list. List must be non-empty.
25-- `(last [1,2,3] == 3)`
29-- `(last [1,2,3] == 3)`
26last : [a] -> a
30last : [a] -> a
28-- Check if a list is empty `(isEmpty [] == True)`
33-- Check if a list is empty `(isEmpty [] == True)`
29isEmpty : [a] -> Bool
34isEmpty : [a] -> Bool
@@ -33,77 +38,92 @@ isEmpty xs =
33 _ -> False
38 _ -> False
35-- Apply a function to every element of a list: `(map sqrt [1,4,9] == [1,2,3])`
40-- Apply a function to every element of a list: `(map sqrt [1,4,9] == [1,2,3])`
36−map : (a -> b) -> [a] -> [b]
41+map : (a -> b) -> [a] -> [b]
38-- Reduce a list from the left: `(foldl (::) [] "gateman" == "nametag")`
44-- Reduce a list from the left: `(foldl (::) [] "gateman" == "nametag")`
39−foldl : (a -> b -> b) -> b -> [a] -> b
45+foldl : (a -> b -> b) -> b -> [a] -> b
41-- Reduce a list from the right: `(foldr (+) 0 [1,2,3] == 6)`
48-- Reduce a list from the right: `(foldr (+) 0 [1,2,3] == 6)`
42−foldr : (a -> b -> b) -> b -> [a] -> b
49+foldr : (a -> b -> b) -> b -> [a] -> b
44-- Reduce a list from the left without a base case. List must be non-empty.
52-- Reduce a list from the left without a base case. List must be non-empty.
45foldl1 : (a -> a -> a) -> [a] -> a
53foldl1 : (a -> a -> a) -> [a] -> a
54+foldl1 = Native.foldl1
47-- Reduce a list from the right without a base case. List must be non-empty.
56-- Reduce a list from the right without a base case. List must be non-empty.
48foldr1 : (a -> a -> a) -> [a] -> a
57foldr1 : (a -> a -> a) -> [a] -> a
58+foldr1 = Native.foldr1
50-- Reduce a list from the left, building up all of the intermediate results into a list.
60-- Reduce a list from the left, building up all of the intermediate results into a list.
52-- scanl (+) 0 [1,2,3,4] == [0,1,3,6,10]
62-- scanl (+) 0 [1,2,3,4] == [0,1,3,6,10]
53−scanl : (a -> b -> b) -> b -> [a] -> [b]
63+scanl : (a -> b -> b) -> b -> [a] -> [b]
55-- Same as scanl but it doesn't require a base case. List must be non-empty.
66-- Same as scanl but it doesn't require a base case. List must be non-empty.
57-- scanl1 (+) [1,2,3,4] == [1,3,6,10]
68-- scanl1 (+) [1,2,3,4] == [1,3,6,10]
58scanl1 : (a -> a -> a) -> [a] -> [a]
69scanl1 : (a -> a -> a) -> [a] -> [a]
70+scanl1 = Native.scanl1
60-- Filter out elements which do not satisfy the predicate: `(filter isLower "AaBbCc" == "abc")`
72-- Filter out elements which do not satisfy the predicate: `(filter isLower "AaBbCc" == "abc")`
61−filter : (a -> Bool) -> [a] -> [a]
73+filter : (a -> Bool) -> [a] -> [a]
74+filter = Native.filter
63-- Determine the length of a list: `(length "innumerable" == 11)`
76-- Determine the length of a list: `(length "innumerable" == 11)`
64−length : [a] -> Int
77+length : [a] -> Int
78+length = Native.length
66-- Reverse a list. `(reverse [1..4] == [4,3,2,1])`
80-- Reverse a list. `(reverse [1..4] == [4,3,2,1])`
67reverse : [a] -> [a]
81reverse : [a] -> [a]
82+reverse = Native.reverse
69-- Check to see if all elements satisfy the predicate.
84-- Check to see if all elements satisfy the predicate.
70all : (a -> Bool) -> [a] -> Bool
85all : (a -> Bool) -> [a] -> Bool
72-- Check to see if any elements satisfy the predicate.
88-- Check to see if any elements satisfy the predicate.
73any : (a -> Bool) -> [a] -> Bool
89any : (a -> Bool) -> [a] -> Bool
75-- Check to see if all elements are True.
92-- Check to see if all elements are True.
76and : [Bool] -> Bool
93and : [Bool] -> Bool
78-- Check to see if any elements are True.
96-- Check to see if any elements are True.
79−or : [Bool] -> Bool
97+or : [Bool] -> Bool
81-- Concatenate a list of appendable things:
100-- Concatenate a list of appendable things:
83-- concat ["tree","house"] == "treehouse"
102-- concat ["tree","house"] == "treehouse"
84−concat : [Appendable a] -> Appendable a
103+concat : [appendable] -> appendable
104+concat = Native.concat
86-- Map a given function onto a list and flatten the resulting lists.
106-- Map a given function onto a list and flatten the resulting lists.
88-- concatMap f xs == concat (map f xs)
108-- concatMap f xs == concat (map f xs)
89−concatMap : (a -> Appendable b) -> [a] -> Appendable b
109+concatMap : (a -> appendable) -> [a] -> appendable
90−concatMap f = L.concat . L.map f
110+concatMap f list = concat (map f list)
92-- Get the sum of the list elements. `(sum [1..4] == 10)`
112-- Get the sum of the list elements. `(sum [1..4] == 10)`
93−sum : [Number a] -> Number a
113+sum : [number] -> number
94−sum = L.foldl (+) 0
114+sum = foldl (+) 0
96-- Get the product of the list elements. `(product [1..4] == 24)`
116-- Get the product of the list elements. `(product [1..4] == 24)`
97−product : [Number a] -> Number a
117+product : [number] -> number
98−product = L.foldl (*) 1
118+product = foldl (*) 1
100−-- Find the highest number in a non-empty list.
120+-- Find the maximum element in a non-empty list: `maximum [1,4,2] == 4`
101−maximum : [Number a] -> Number a
121+maximum : [comparable] -> comparable
102−maximum = L.foldl1 max
122+maximum = foldl1 Native.Utils.max
104−-- Find the lowest number in a non-empty list.
124+-- Find the minimum element in a non-empty list: `minimum [3,2,1] == 1`
105−minimum : [Number a] -> Number a
125+minimum : [comparable] -> comparable
106−minimum = L.foldl1 min
126+minimum = foldl1 Native.Utils.min
108-- Split a list based on the predicate.
128-- Split a list based on the predicate.
109partition : (a -> Bool) -> [a] -> ([a],[a])
129partition : (a -> Bool) -> [a] -> ([a],[a])
@@ -119,12 +139,14 @@ partition pred lst =
119-- zip [1,2,3] [6,7] == [(1,6),(2,7)]
139-- zip [1,2,3] [6,7] == [(1,6),(2,7)]
120-- zip == zipWith (,)
140-- zip == zipWith (,)
121zip : [a] -> [b] -> [(a,b)]
141zip : [a] -> [b] -> [(a,b)]
123-- Combine two lists, combining them with the given function.
144-- Combine two lists, combining them with the given function.
124-- If one list is longer, the extra elements are dropped.
145-- If one list is longer, the extra elements are dropped.
126-- zipWith (+) [1,2,3] [1,2,3,4] == [2,4,6]
147-- zipWith (+) [1,2,3] [1,2,3,4] == [2,4,6]
127zipWith : (a -> b -> c) -> [a] -> [b] -> [c]
148zipWith : (a -> b -> c) -> [a] -> [b] -> [c]
149+zipWith = Native.zipWith
129-- Decompose a list of tuples.
151-- Decompose a list of tuples.
130unzip : [(a,b)] -> ([a],[b])
152unzip : [(a,b)] -> ([a],[b])
@@ -137,12 +159,14 @@ unzip pairs =
138-- split "," "hello,there,friend" == ["hello", "there", "friend"]
160-- split "," "hello,there,friend" == ["hello", "there", "friend"]
139split : [a] -> [a] -> [[a]]
161split : [a] -> [a] -> [[a]]
141-- Places the given value between all of the lists in the second argument
164-- Places the given value between all of the lists in the second argument
142-- and concatenates the result.
165-- and concatenates the result.
144-- join "a" ["H","w","ii","n"] == "Hawaiian"
167-- join "a" ["H","w","ii","n"] == "Hawaiian"
145−join : Appendable a -> [Appendable a] -> Appendable a
168+join : appendable -> [appendable] -> appendable
147-- Places the given value between all members of the given list.
171-- Places the given value between all members of the given list.
@@ -156,7 +180,8 @@ intersperse sep xs =
157-- Take the first n members of a list: `(take 2 [1,2,3,4] == [1,2])`
181-- Take the first n members of a list: `(take 2 [1,2,3,4] == [1,2])`
158take : Int -> [a] -> [a]
182take : Int -> [a] -> [a]
160-- Drop the first n members of a list: `(drop 2 [1,2,3,4] == [3,4])`
185-- Drop the first n members of a list: `(drop 2 [1,2,3,4] == [3,4])`
161drop : Int -> [a] -> [a]
186drop : Int -> [a] -> [a]
162−
187+drop = Native.drop