on a3801ac help sign in
elk/libraries/List.elm
162 lines 4.8 kB history raw
2module List where
4import Native.Utils (min, max)
5import Native.List as L
7-- Add an element to the front of a list `(1 :: [2,3] == [1,2,3])`
8(::) : a -> [a] -> [a]
10-- Puts two appendable things together:
11--
12-- [1,1] ++ [2,3] == [1,1,2,3]
13-- "abc" ++ "123" == "abc123"
14(++) : Appendable a -> Appendable a -> Appendable a
16-- Extract the first element of a list. List must be non-empty.
17-- `(head [1,2,3] == 1)`
18head : [a] -> a
20-- Extract the elements after the head of the list. List must be non-empty.
21-- `(tail [1,2,3] == [2,3])`
22tail : [a] -> [a]
24-- Extract the last element of a list. List must be non-empty.
25-- `(last [1,2,3] == 3)`
26last : [a] -> a
28-- Check if a list is empty `(isEmpty [] == True)`
29isEmpty : [a] -> Bool
30isEmpty xs =
31 case xs of
32 [] -> True
33 _ -> False
35-- Apply a function to every element of a list: `(map sqrt [1,4,9] == [1,2,3])`
36map : (a -> b) -> [a] -> [b]
38-- Reduce a list from the left: `(foldl (::) [] "gateman" == "nametag")`
39foldl : (a -> b -> b) -> b -> [a] -> b
41-- Reduce a list from the right: `(foldr (+) 0 [1,2,3] == 6)`
42foldr : (a -> b -> b) -> b -> [a] -> b
44-- Reduce a list from the left without a base case. List must be non-empty.
45foldl1 : (a -> a -> a) -> [a] -> a
47-- Reduce a list from the right without a base case. List must be non-empty.
48foldr1 : (a -> a -> a) -> [a] -> a
50-- Reduce a list from the left, building up all of the intermediate results into a list.
51--
52-- scanl (+) 0 [1,2,3,4] == [0,1,3,6,10]
53scanl : (a -> b -> b) -> b -> [a] -> [b]
55-- Same as scanl but it doesn't require a base case. List must be non-empty.
56--
57-- scanl1 (+) [1,2,3,4] == [1,3,6,10]
58scanl1 : (a -> a -> a) -> [a] -> [a]
60-- Filter out elements which do not satisfy the predicate: `(filter isLower "AaBbCc" == "abc")`
61filter : (a -> Bool) -> [a] -> [a]
63-- Determine the length of a list: `(length "innumerable" == 11)`
64length : [a] -> Int
66-- Reverse a list. `(reverse [1..4] == [4,3,2,1])`
67reverse : [a] -> [a]
69-- Check to see if all elements satisfy the predicate.
70all : (a -> Bool) -> [a] -> Bool
72-- Check to see if any elements satisfy the predicate.
73any : (a -> Bool) -> [a] -> Bool
75-- Check to see if all elements are True.
76and : [Bool] -> Bool
78-- Check to see if any elements are True.
79or : [Bool] -> Bool
81-- Concatenate a list of appendable things:
82--
83-- concat ["tree","house"] == "treehouse"
84concat : [Appendable a] -> Appendable a
86-- Map a given function onto a list and flatten the resulting lists.
87--
88-- concatMap f xs == concat (map f xs)
89concatMap : (a -> Appendable b) -> [a] -> Appendable b
90concatMap f = L.concat . L.map f
92-- Get the sum of the list elements. `(sum [1..4] == 10)`
93sum : [Number a] -> Number a
94sum = L.foldl (+) 0
96-- Get the product of the list elements. `(product [1..4] == 24)`
97product : [Number a] -> Number a
98product = L.foldl (*) 1
100-- Find the highest number in a non-empty list.
101maximum : [Number a] -> Number a
102maximum = L.foldl1 max
104-- Find the lowest number in a non-empty list.
105minimum : [Number a] -> Number a
106minimum = L.foldl1 min
108-- Split a list based on the predicate.
109partition : (a -> Bool) -> [a] -> ([a],[a])
110partition pred lst =
111 case lst of
112 [] -> ([],[])
113 x::xs -> let (bs,cs) = partition pred xs in
114 if pred x then (x::bs,cs) else (bs,x::cs)
116-- Combine two lists, combining them into tuples pairwise.
117-- If one list is longer, the extra elements are dropped.
118--
119-- zip [1,2,3] [6,7] == [(1,6),(2,7)]
120-- zip == zipWith (,)
121zip : [a] -> [b] -> [(a,b)]
123-- Combine two lists, combining them with the given function.
124-- If one list is longer, the extra elements are dropped.
125--
126-- zipWith (+) [1,2,3] [1,2,3,4] == [2,4,6]
127zipWith : (a -> b -> c) -> [a] -> [b] -> [c]
129-- Decompose a list of tuples.
130unzip : [(a,b)] -> ([a],[b])
131unzip pairs =
132 case pairs of
133 [] -> ([],[])
134 (x,y)::ps -> let (xs,ys) = (unzip ps) in (x::xs,y::ys)
136-- Split a list with a given seperator.
137--
138-- split "," "hello,there,friend" == ["hello", "there", "friend"]
139split : [a] -> [a] -> [[a]]
141-- Places the given value between all of the lists in the second argument
142-- and concatenates the result.
143--
144-- join "a" ["H","w","ii","n"] == "Hawaiian"
145join : Appendable a -> [Appendable a] -> Appendable a
147-- Places the given value between all members of the given list.
148--
149-- intersperse ' ' "INCEPTION" == "I N C E P T I O N"
150intersperse : a -> [a] -> [a]
151intersperse sep xs =
152 case xs of
153 a::b::cs -> a :: sep :: intersperse sep (b::cs)
154 [a] -> [a]
155 [] -> []
157-- Take the first n members of a list: `(take 2 [1,2,3,4] == [1,2])`
158take : Int -> [a] -> [a]
160-- Drop the first n members of a list: `(drop 2 [1,2,3,4] == [3,4])`
161drop : Int -> [a] -> [a]