14.11.2012 Views

Curry: An Integrated Functional Logic Language

Curry: An Integrated Functional Logic Language

Curry: An Integrated Functional Logic Language

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

unzip3 :: [(a,b,c)] -> ([a],[b],[c])<br />

unzip3 [] = ([],[],[])<br />

unzip3 ((x,y,z):ts) = (x:xs,y:ys,z:zs) where (xs,ys,zs) = unzip3 ts<br />

-- Concatenate a list of lists into one list<br />

concat :: [[a]] -> [a]<br />

concat l = foldr (++) [] l<br />

-- Map a function from elements to lists and merge the result into one list<br />

concatMap :: (a -> [b]) -> [a] -> [b]<br />

concatMap f = concat . map f<br />

-- Infinite list of repeated applications of a function f to an element x:<br />

-- iterate f x = [x, f x, f (f x),...]<br />

iterate :: (a -> a) -> a -> [a]<br />

iterate f x = x : iterate f (f x)<br />

-- Infinite list where all elements have the same value x:<br />

repeat :: a -> [a]<br />

repeat x = x : repeat x<br />

-- List of length n where all elements have the same value x:<br />

replicate :: Int -> a -> [a]<br />

replicate n x = take n (repeat x)<br />

-- Return prefix of length n<br />

take :: Int -> [a] -> [a]<br />

take n l = if n==0 then [] else takep n l<br />

where takep _ [] = []<br />

takep n (x:xs) = x : take (n-1) xs<br />

-- Return suffix without first n elements<br />

drop :: Int -> [a] -> [a]<br />

drop n l = if n==0 then l else dropp n l<br />

where dropp _ [] = []<br />

dropp n (_:xs) = drop (n-1) xs<br />

-- (splitAt n xs) is equivalent to (take n xs, drop n xs)<br />

splitAt :: Int -> [a] -> ([a],[a])<br />

splitAt n l = if n==0 then ([],l) else splitAtp n l<br />

where splitAtp _ [] = ([],[])<br />

splitAtp n (x:xs) = let (ys,zs) = splitAt (n-1) xs in (x:ys,zs)<br />

-- Return longest prefix with elements satisfying a predicate<br />

takeWhile :: (a -> Bool) -> [a] -> [a]<br />

51

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!