2012-10-05 02:01:20 +00:00
|
|
|
|
2013-02-09 06:17:39 +00:00
|
|
|
module List where
|
2012-10-05 02:01:20 +00:00
|
|
|
|
2013-03-12 07:48:11 +00:00
|
|
|
import Native.Utils (min, max)
|
2013-03-03 15:55:37 +00:00
|
|
|
import Native.List as L
|
2012-10-05 02:01:20 +00:00
|
|
|
|
2013-05-17 20:46:08 +00:00
|
|
|
-- Add an element to the front of a list `(1 :: [2,3] == [1,2,3])`
|
2013-04-03 07:33:55 +00:00
|
|
|
(::) : a -> [a] -> [a]
|
|
|
|
|
2013-05-17 20:46:08 +00:00
|
|
|
-- Puts two appendable things together:
|
|
|
|
--
|
2013-05-28 13:46:29 +00:00
|
|
|
-- [1,1] ++ [2,3] == [1,1,2,3]
|
|
|
|
-- "abc" ++ "123" == "abc123"
|
2013-04-08 08:49:44 +00:00
|
|
|
(++) : Appendable a -> Appendable a -> Appendable a
|
2013-04-03 07:33:55 +00:00
|
|
|
|
2013-03-24 12:45:56 +00:00
|
|
|
-- Extract the first element of a list. List must be non-empty.
|
2013-05-17 20:46:08 +00:00
|
|
|
-- `(head [1,2,3] == 1)`
|
2013-02-09 06:17:39 +00:00
|
|
|
head : [a] -> a
|
2013-03-24 12:45:56 +00:00
|
|
|
|
|
|
|
-- Extract the elements after the head of the list. List must be non-empty.
|
2013-05-17 20:46:08 +00:00
|
|
|
-- `(tail [1,2,3] == [2,3])`
|
2013-02-09 06:17:39 +00:00
|
|
|
tail : [a] -> [a]
|
2013-03-24 12:45:56 +00:00
|
|
|
|
|
|
|
-- Extract the last element of a list. List must be non-empty.
|
2013-05-17 20:46:08 +00:00
|
|
|
-- `(last [1,2,3] == 3)`
|
2013-02-10 09:45:45 +00:00
|
|
|
last : [a] -> a
|
2013-03-24 12:45:56 +00:00
|
|
|
|
2013-05-28 13:46:29 +00:00
|
|
|
-- Check if a list is empty `(isEmpty [] == True)`
|
|
|
|
isEmpty : [a] -> Bool
|
|
|
|
isEmpty xs =
|
|
|
|
case xs of
|
|
|
|
[] -> True
|
|
|
|
_ -> False
|
|
|
|
|
2013-05-17 20:46:08 +00:00
|
|
|
-- Apply a function to every element of a list: `(map sqrt [1,4,9] == [1,2,3])`
|
2013-03-10 08:12:10 +00:00
|
|
|
map : (a -> b) -> [a] -> [b]
|
2013-03-24 12:45:56 +00:00
|
|
|
|
2013-05-17 20:46:08 +00:00
|
|
|
-- Reduce a list from the left: `(foldl (::) [] "gateman" == "nametag")`
|
2013-03-10 08:12:10 +00:00
|
|
|
foldl : (a -> b -> b) -> b -> [a] -> b
|
2013-03-24 12:45:56 +00:00
|
|
|
|
2013-05-17 20:46:08 +00:00
|
|
|
-- Reduce a list from the right: `(foldr (+) 0 [1,2,3] == 6)`
|
2013-03-10 08:12:10 +00:00
|
|
|
foldr : (a -> b -> b) -> b -> [a] -> b
|
2013-03-24 12:45:56 +00:00
|
|
|
|
|
|
|
-- Reduce a list from the left without a base case. List must be non-empty.
|
2013-02-09 06:17:39 +00:00
|
|
|
foldl1 : (a -> a -> a) -> [a] -> a
|
2013-03-24 12:45:56 +00:00
|
|
|
|
|
|
|
-- Reduce a list from the right without a base case. List must be non-empty.
|
2013-02-09 06:17:39 +00:00
|
|
|
foldr1 : (a -> a -> a) -> [a] -> a
|
2013-02-10 04:36:57 +00:00
|
|
|
|
2013-03-24 12:45:56 +00:00
|
|
|
-- Reduce a list from the left, building up all of the intermediate results into a list.
|
2013-05-17 20:46:08 +00:00
|
|
|
--
|
2013-05-28 13:46:29 +00:00
|
|
|
-- scanl (+) 0 [1,2,3,4] == [0,1,3,6,10]
|
2013-03-10 08:12:10 +00:00
|
|
|
scanl : (a -> b -> b) -> b -> [a] -> [b]
|
2013-03-24 12:45:56 +00:00
|
|
|
|
|
|
|
-- Same as scanl but it doesn't require a base case. List must be non-empty.
|
2013-05-17 20:46:08 +00:00
|
|
|
--
|
2013-05-28 13:46:29 +00:00
|
|
|
-- scanl1 (+) [1,2,3,4] == [1,3,6,10]
|
2013-02-10 04:36:57 +00:00
|
|
|
scanl1 : (a -> a -> a) -> [a] -> [a]
|
2013-02-09 06:17:39 +00:00
|
|
|
|
2013-05-17 20:46:08 +00:00
|
|
|
-- Filter out elements which do not satisfy the predicate: `(filter isLower "AaBbCc" == "abc")`
|
2013-03-10 08:12:10 +00:00
|
|
|
filter : (a -> Bool) -> [a] -> [a]
|
2013-03-24 12:45:56 +00:00
|
|
|
|
2013-05-17 20:46:08 +00:00
|
|
|
-- Determine the length of a list: `(length "innumerable" == 11)`
|
2013-03-10 08:12:10 +00:00
|
|
|
length : [a] -> Int
|
2013-03-24 12:45:56 +00:00
|
|
|
|
2013-05-17 20:46:08 +00:00
|
|
|
-- Reverse a list. `(reverse [1..4] == [4,3,2,1])`
|
2013-02-09 06:17:39 +00:00
|
|
|
reverse : [a] -> [a]
|
|
|
|
|
2013-03-24 12:45:56 +00:00
|
|
|
-- Check to see if all elements satisfy the predicate.
|
2013-02-10 09:45:45 +00:00
|
|
|
all : (a -> Bool) -> [a] -> Bool
|
2013-03-24 12:45:56 +00:00
|
|
|
|
|
|
|
-- Check to see if any elements satisfy the predicate.
|
2013-02-09 06:17:39 +00:00
|
|
|
any : (a -> Bool) -> [a] -> Bool
|
2013-03-24 12:45:56 +00:00
|
|
|
|
|
|
|
-- Check to see if all elements are True.
|
2013-02-10 09:45:45 +00:00
|
|
|
and : [Bool] -> Bool
|
2013-03-24 12:45:56 +00:00
|
|
|
|
|
|
|
-- Check to see if any elements are True.
|
2013-03-10 08:12:10 +00:00
|
|
|
or : [Bool] -> Bool
|
2013-02-10 09:45:45 +00:00
|
|
|
|
2013-05-17 20:46:08 +00:00
|
|
|
-- Concatenate a list of appendable things:
|
|
|
|
--
|
2013-05-28 13:46:29 +00:00
|
|
|
-- concat ["tree","house"] == "treehouse"
|
2013-04-08 08:49:44 +00:00
|
|
|
concat : [Appendable a] -> Appendable a
|
2013-03-24 12:45:56 +00:00
|
|
|
|
2013-04-10 05:15:06 +00:00
|
|
|
-- Map a given function onto a list and flatten the resulting lists.
|
|
|
|
--
|
2013-05-28 13:46:29 +00:00
|
|
|
-- concatMap f xs == concat (map f xs)
|
2013-04-08 08:49:44 +00:00
|
|
|
concatMap : (a -> Appendable b) -> [a] -> Appendable b
|
2013-03-10 08:12:10 +00:00
|
|
|
concatMap f = L.concat . L.map f
|
2012-10-05 02:01:20 +00:00
|
|
|
|
2013-05-17 20:46:08 +00:00
|
|
|
-- Get the sum of the list elements. `(sum [1..4] == 10)`
|
2013-04-22 09:36:11 +00:00
|
|
|
sum : [Number a] -> Number a
|
2013-03-10 08:12:10 +00:00
|
|
|
sum = L.foldl (+) 0
|
2013-03-24 12:45:56 +00:00
|
|
|
|
2013-05-17 20:46:08 +00:00
|
|
|
-- Get the product of the list elements. `(product [1..4] == 24)`
|
2013-04-22 09:36:11 +00:00
|
|
|
product : [Number a] -> Number a
|
2013-03-10 08:12:10 +00:00
|
|
|
product = L.foldl (*) 1
|
2013-03-24 12:45:56 +00:00
|
|
|
|
|
|
|
-- Find the highest number in a non-empty list.
|
2013-05-17 20:46:08 +00:00
|
|
|
maximum : [Number a] -> Number a
|
2013-03-10 08:12:10 +00:00
|
|
|
maximum = L.foldl1 max
|
2013-03-24 12:45:56 +00:00
|
|
|
|
|
|
|
-- Find the lowest number in a non-empty list.
|
2013-05-17 20:46:08 +00:00
|
|
|
minimum : [Number a] -> Number a
|
2013-03-10 08:12:10 +00:00
|
|
|
minimum = L.foldl1 min
|
2012-10-05 02:01:20 +00:00
|
|
|
|
2013-03-24 12:45:56 +00:00
|
|
|
-- Split a list based on the predicate.
|
2013-02-09 06:17:39 +00:00
|
|
|
partition : (a -> Bool) -> [a] -> ([a],[a])
|
2012-10-05 02:01:20 +00:00
|
|
|
partition pred lst =
|
|
|
|
case lst of
|
2013-02-09 06:17:39 +00:00
|
|
|
[] -> ([],[])
|
2013-02-27 07:26:28 +00:00
|
|
|
x::xs -> let (bs,cs) = partition pred xs in
|
|
|
|
if pred x then (x::bs,cs) else (bs,x::cs)
|
2012-10-05 02:01:20 +00:00
|
|
|
|
2013-05-17 20:46:08 +00:00
|
|
|
-- Combine two lists, combining them into tuples pairwise.
|
|
|
|
-- If one list is longer, the extra elements are dropped.
|
|
|
|
--
|
2013-05-28 13:46:29 +00:00
|
|
|
-- zip [1,2,3] [6,7] == [(1,6),(2,7)]
|
|
|
|
-- zip == zipWith (,)
|
2013-02-09 06:17:39 +00:00
|
|
|
zip : [a] -> [b] -> [(a,b)]
|
2012-10-05 02:01:20 +00:00
|
|
|
|
2013-05-17 20:46:08 +00:00
|
|
|
-- Combine two lists, combining them with the given function.
|
|
|
|
-- If one list is longer, the extra elements are dropped.
|
|
|
|
--
|
2013-05-28 13:46:29 +00:00
|
|
|
-- zipWith (+) [1,2,3] [1,2,3,4] == [2,4,6]
|
2013-03-24 12:45:56 +00:00
|
|
|
zipWith : (a -> b -> c) -> [a] -> [b] -> [c]
|
|
|
|
|
2013-05-17 20:46:08 +00:00
|
|
|
-- Decompose a list of tuples.
|
2013-02-09 06:17:39 +00:00
|
|
|
unzip : [(a,b)] -> ([a],[b])
|
2012-10-05 02:01:20 +00:00
|
|
|
unzip pairs =
|
|
|
|
case pairs of
|
2013-02-09 06:17:39 +00:00
|
|
|
[] -> ([],[])
|
|
|
|
(x,y)::ps -> let (xs,ys) = (unzip ps) in (x::xs,y::ys)
|
2012-10-05 02:01:20 +00:00
|
|
|
|
2013-05-17 10:49:31 +00:00
|
|
|
-- Split a list with a given seperator.
|
2013-04-22 09:36:11 +00:00
|
|
|
--
|
2013-05-28 13:46:29 +00:00
|
|
|
-- split "," "hello,there,friend" == ["hello", "there", "friend"]
|
2013-03-21 09:29:23 +00:00
|
|
|
split : [a] -> [a] -> [[a]]
|
2013-03-24 12:45:56 +00:00
|
|
|
|
2013-04-10 07:52:53 +00:00
|
|
|
-- Places the given value between all of the lists in the second argument
|
|
|
|
-- and concatenates the result.
|
|
|
|
--
|
2013-05-28 13:46:29 +00:00
|
|
|
-- join "a" ["H","w","ii","n"] == "Hawaiian"
|
2013-04-10 07:52:53 +00:00
|
|
|
join : Appendable a -> [Appendable a] -> Appendable a
|
2013-03-21 09:29:23 +00:00
|
|
|
|
2013-03-24 12:45:56 +00:00
|
|
|
-- Places the given value between all members of the given list.
|
2013-05-17 20:46:08 +00:00
|
|
|
--
|
2013-05-28 13:46:29 +00:00
|
|
|
-- intersperse ' ' "INCEPTION" == "I N C E P T I O N"
|
2013-03-21 09:29:23 +00:00
|
|
|
intersperse : a -> [a] -> [a]
|
2012-10-05 02:01:20 +00:00
|
|
|
intersperse sep xs =
|
2013-02-09 06:17:39 +00:00
|
|
|
case xs of
|
|
|
|
a::b::cs -> a :: sep :: intersperse sep (b::cs)
|
|
|
|
[a] -> [a]
|
|
|
|
[] -> []
|
|
|
|
|
2013-05-17 20:46:08 +00:00
|
|
|
-- Take the first n members of a list: `(take 2 [1,2,3,4] == [1,2])`
|
2013-03-24 12:45:56 +00:00
|
|
|
take : Int -> [a] -> [a]
|
|
|
|
|
2013-05-17 20:46:08 +00:00
|
|
|
-- Drop the first n members of a list: `(drop 2 [1,2,3,4] == [3,4])`
|
2013-03-24 12:45:56 +00:00
|
|
|
drop : Int -> [a] -> [a]
|
|
|
|
|