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