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-07-25 14:40:39 +00:00
|
|
|
import Native.Utils
|
|
|
|
import Native.List as Native
|
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-07-25 14:40:39 +00:00
|
|
|
(::) = Native.cons
|
2013-04-03 07:33:55 +00:00
|
|
|
|
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-07-25 14:40:39 +00:00
|
|
|
(++) : appendable -> appendable -> appendable
|
|
|
|
(++) = Native.append
|
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-07-25 14:40:39 +00:00
|
|
|
head = Native.head
|
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-07-25 14:40:39 +00:00
|
|
|
tail = Native.tail
|
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-07-25 14:40:39 +00:00
|
|
|
last = Native.last
|
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-07-25 14:40:39 +00:00
|
|
|
map : (a -> b) -> [a] -> [b]
|
|
|
|
map = Native.map
|
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-07-25 14:40:39 +00:00
|
|
|
foldl : (a -> b -> b) -> b -> [a] -> b
|
|
|
|
foldl = Native.foldl
|
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-07-25 14:40:39 +00:00
|
|
|
foldr : (a -> b -> b) -> b -> [a] -> b
|
|
|
|
foldr = Native.foldr
|
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-07-25 14:40:39 +00:00
|
|
|
foldl1 = Native.foldl1
|
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-07-25 14:40:39 +00:00
|
|
|
foldr1 = Native.foldr1
|
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-07-25 14:40:39 +00:00
|
|
|
scanl : (a -> b -> b) -> b -> [a] -> [b]
|
|
|
|
scanl = Native.scanl
|
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-07-25 14:40:39 +00:00
|
|
|
scanl1 = Native.scanl1
|
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-07-25 14:40:39 +00:00
|
|
|
filter : (a -> Bool) -> [a] -> [a]
|
|
|
|
filter = Native.filter
|
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-07-25 14:40:39 +00:00
|
|
|
length : [a] -> Int
|
|
|
|
length = Native.length
|
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-07-25 14:40:39 +00:00
|
|
|
reverse = Native.reverse
|
2013-02-09 06:17:39 +00:00
|
|
|
|
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-07-25 14:40:39 +00:00
|
|
|
all = Native.all
|
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-07-25 14:40:39 +00:00
|
|
|
any = Native.any
|
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-07-25 14:40:39 +00:00
|
|
|
and = Native.and
|
2013-03-24 12:45:56 +00:00
|
|
|
|
|
|
|
-- Check to see if any elements are True.
|
2013-07-25 14:40:39 +00:00
|
|
|
or : [Bool] -> Bool
|
|
|
|
or = Native.or
|
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-07-25 14:40:39 +00:00
|
|
|
concat : [appendable] -> appendable
|
|
|
|
concat = Native.concat
|
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-07-25 14:40:39 +00:00
|
|
|
concatMap : (a -> appendable) -> [a] -> appendable
|
|
|
|
concatMap f list = concat (map f list)
|
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-07-25 14:40:39 +00:00
|
|
|
sum : [number] -> number
|
|
|
|
sum = 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-07-25 14:40:39 +00:00
|
|
|
product : [number] -> number
|
|
|
|
product = foldl (*) 1
|
2013-03-24 12:45:56 +00:00
|
|
|
|
2013-07-25 14:40:39 +00:00
|
|
|
-- Find the maximum element in a non-empty list: `maximum [1,4,2] == 4`
|
|
|
|
maximum : [comparable] -> comparable
|
|
|
|
maximum = foldl1 Native.Utils.max
|
2013-03-24 12:45:56 +00:00
|
|
|
|
2013-07-25 14:40:39 +00:00
|
|
|
-- Find the minimum element in a non-empty list: `minimum [3,2,1] == 1`
|
|
|
|
minimum : [comparable] -> comparable
|
|
|
|
minimum = foldl1 Native.Utils.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)]
|
2013-07-25 14:40:39 +00:00
|
|
|
zip = Native.zip
|
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-07-25 14:40:39 +00:00
|
|
|
zipWith = Native.zipWith
|
2013-03-24 12:45:56 +00:00
|
|
|
|
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-07-25 14:40:39 +00:00
|
|
|
split = Native.split
|
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-07-25 14:40:39 +00:00
|
|
|
join : appendable -> [appendable] -> appendable
|
|
|
|
join = Native.join
|
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-07-25 14:40:39 +00:00
|
|
|
take = Native.take
|
2013-03-24 12:45:56 +00:00
|
|
|
|
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]
|
2013-07-25 14:40:39 +00:00
|
|
|
drop = Native.drop
|