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-04-03 07:33:55 +00:00
|
|
|
-- Add an element to the front of a list
|
|
|
|
-- a :: [b,c] = [a,b,c]
|
|
|
|
(::) : a -> [a] -> [a]
|
|
|
|
|
|
|
|
-- Appends two lists.
|
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-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-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-02-10 09:45:45 +00:00
|
|
|
last : [a] -> a
|
2013-03-24 12:45:56 +00:00
|
|
|
|
|
|
|
-- Apply a function to every element of a list.
|
2013-03-10 08:12:10 +00:00
|
|
|
map : (a -> b) -> [a] -> [b]
|
2013-03-24 12:45:56 +00:00
|
|
|
|
|
|
|
-- Reduce a list from the left.
|
2013-03-10 08:12:10 +00:00
|
|
|
foldl : (a -> b -> b) -> b -> [a] -> b
|
2013-03-24 12:45:56 +00:00
|
|
|
|
|
|
|
-- Reduce a list from the right.
|
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-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-02-10 04:36:57 +00:00
|
|
|
scanl1 : (a -> a -> a) -> [a] -> [a]
|
2013-02-09 06:17:39 +00:00
|
|
|
|
2013-03-24 12:45:56 +00:00
|
|
|
-- Filter out elements which do not satisfy the predicate.
|
2013-03-10 08:12:10 +00:00
|
|
|
filter : (a -> Bool) -> [a] -> [a]
|
2013-03-24 12:45:56 +00:00
|
|
|
|
|
|
|
-- Determine the length of a list.
|
2013-03-10 08:12:10 +00:00
|
|
|
length : [a] -> Int
|
2013-03-24 12:45:56 +00:00
|
|
|
|
|
|
|
-- Reverse a list.
|
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-03-24 12:45:56 +00:00
|
|
|
-- Flatten a list of lists.
|
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.
|
|
|
|
--
|
|
|
|
-- 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-03-24 12:45:56 +00:00
|
|
|
-- Get the sum of the list elements.
|
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
|
|
|
|
|
|
|
-- Get the product of the list elements.
|
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-04-22 09:36:11 +00:00
|
|
|
maximum : [Comparable a] -> Comparable 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-04-22 09:36:11 +00:00
|
|
|
minimum : [Comparable a] -> Comparable 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-03-24 12:45:56 +00:00
|
|
|
-- Combine two lists, combining them into tuples pairwise. If one input list has extra elements (it is longer), those elements are dropped.
|
2013-02-09 06:17:39 +00:00
|
|
|
zip : [a] -> [b] -> [(a,b)]
|
2012-10-05 02:01:20 +00:00
|
|
|
|
2013-03-24 12:45:56 +00:00
|
|
|
-- Combine two lists, combining them with the given function. If one input list has extra elements (it is longer), those elements are dropped.
|
|
|
|
zipWith : (a -> b -> c) -> [a] -> [b] -> [c]
|
|
|
|
|
|
|
|
-- 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-03-24 12:45:56 +00:00
|
|
|
-- Split a list
|
2013-04-22 09:36:11 +00:00
|
|
|
--
|
2013-03-24 12:45:56 +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-03-24 12:45:56 +00:00
|
|
|
-- join xs xss = concat (intersperse xs xss)
|
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-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-03-24 12:45:56 +00:00
|
|
|
-- Take the first n members of a list.
|
|
|
|
-- take 2 [1,2,3,4]) ==> [1,2]
|
|
|
|
take : Int -> [a] -> [a]
|
|
|
|
|
|
|
|
-- Drop the first n members of a list.
|
|
|
|
-- drop 2 [1,2,3,4]) ==> [3,4]
|
|
|
|
drop : Int -> [a] -> [a]
|
|
|
|
|