Curry: An Integrated Functional Logic Language
Curry: An Integrated Functional Logic Language
Curry: An Integrated Functional Logic Language
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
-- Is a list empty?<br />
null :: [_] -> Bool<br />
null [] = True<br />
null (_:_) = False<br />
-- Concatenation<br />
(++) :: [a] -> [a] -> [a]<br />
[] ++ ys = ys<br />
(x:xs) ++ ys = x : xs++ys<br />
-- List length<br />
length :: [a] -> Int<br />
length [] = 0<br />
length (_:xs) = 1 + length xs<br />
-- List index (subscript) operator, head has index 0<br />
(!!) :: [a] -> Int -> a<br />
(x:xs) !! n | n==0 = x<br />
| n>0 = xs !! (n-1)<br />
-- Map a function on a list<br />
map :: (a->b) -> [a] -> [b]<br />
map _ [] = []<br />
map f (x:xs) = f x : map f xs<br />
-- Accumulate all list elements by applying a binary operator from<br />
-- left to right, i.e.,<br />
-- foldl f z [x1,x2,...,xn] = (...((z ‘f‘ x1) ‘f‘ x2) ...) ‘f‘ xn :<br />
foldl :: (a -> b -> a) -> a -> [b] -> a<br />
foldl _ z [] = z<br />
foldl f z (x:xs) = foldl f (f z x) xs<br />
-- Accumulate a non-empty list from left to right:<br />
foldl1 :: (a -> a -> a) -> [a] -> a<br />
foldl1 f (x:xs) = foldl f x xs<br />
-- Accumulate all list elements by applying a binary operator from<br />
-- right to left, i.e.,<br />
-- foldr f z [x1,x2,...,xn] = (x1 ‘f‘ (x2 ‘f‘ ... (xn ‘f‘ z)...)) :<br />
foldr :: (a->b->b) -> b -> [a] -> b<br />
foldr _ z [] = z<br />
foldr f z (x:xs) = f x (foldr f z xs)<br />
-- Accumulate a non-empty list from right to left:<br />
foldr1 :: (a -> a -> a) -> [a] -> a<br />
49