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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

foldr1 _ [x] = x<br />

foldr1 f (x1:x2:xs) = f x1 (foldr1 f (x2:xs))<br />

-- Filter elements in a list<br />

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

filter _ [] = []<br />

filter p (x:xs) = if p x then x : filter p xs<br />

else filter p xs<br />

-- Join two lists into one list of pairs. If one input list is shorter than<br />

-- the other, the additional elements of the longer list are discarded.<br />

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

zip [] _ = []<br />

zip (_:_) [] = []<br />

zip (x:xs) (y:ys) = (x,y) : zip xs ys<br />

-- Join three lists into one list of triples. If one input list is shorter than<br />

-- the other, the additional elements of the longer lists are discarded.<br />

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

zip3 [] _ _ = []<br />

zip3 (_:_) [] _ = []<br />

zip3 (_:_) (_:_) [] = []<br />

zip3 (x:xs) (y:ys) (z:zs) = (x,y,z) : zip3 xs ys zs<br />

-- Join two lists into one list by applying a combination function to<br />

-- corresponding pairs of elements (i.e., zip = zipWith (,)):<br />

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

zipWith _ [] _ = []<br />

zipWith _ (_:_) [] = []<br />

zipWith f (x:xs) (y:ys) = f x y : zipWith f xs ys<br />

-- Join three lists into one list by applying a combination function to<br />

-- corresponding triples of elements (i.e., zip3 = zipWith3 (,,)):<br />

zipWith3 :: (a->b->c->d) -> [a] -> [b] -> [c] -> [d]<br />

zipWith3 _ [] _ _ = []<br />

zipWith3 _ (_:_) [] _ = []<br />

zipWith3 _ (_:_) (_:_) [] = []<br />

zipWith3 f (x:xs) (y:ys) (z:zs) = f x y z : zipWith3 f xs ys zs<br />

-- Transform a list of pairs into a pair of lists<br />

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

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

unzip ((x,y):ps) = (x:xs,y:ys) where (xs,ys) = unzip ps<br />

-- Transform a list of triples into a triple of lists<br />

50

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

Saved successfully!

Ooh no, something went wrong!