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.

takeWhile _ [] = []<br />

takeWhile p (x:xs) = if p x then x : takeWhile p xs else []<br />

-- Return suffix without takeWhile prefix<br />

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

dropWhile _ [] = []<br />

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

-- (span p xs) is equivalent to (takeWhile p xs, dropWhile p xs)<br />

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

span _ [] = ([],[])<br />

span p (x:xs)<br />

| p x = let (ys,zs) = span p xs in (x:ys, zs)<br />

| otherwise = ([],x:xs)<br />

-- (break p xs) is equivalent to (takeWhile (not.p) xs, dropWhile (not.p) xs)<br />

-- i.e., it breaks a list at the first occurrence of an element satisfying p<br />

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

break p = span (not . p)<br />

-- Break a string into list of lines where a line is terminated at a<br />

-- newline character. The resulting lines do not contain newline characters.<br />

lines :: String -> [String]<br />

lines [] = []<br />

lines (c:cs) = let (l,restcs) = splitline (c:cs) in l : lines restcs<br />

where splitline [] = ([],[])<br />

splitline (c:cs) = if c==’\n’<br />

then ([],cs)<br />

else let (ds,es) = splitline cs in (c:ds,es)<br />

-- Concatenate a list of strings with terminating newlines<br />

unlines :: [String] -> String<br />

unlines ls = concatMap (++"\n") ls<br />

-- Break a string into a list of words where the words are delimited by<br />

-- white spaces.<br />

words :: String -> [String]<br />

words s = let s1 = dropWhile isSpace s<br />

in if s1=="" then []<br />

else let (w,s2) = break isSpace s1<br />

in w : words s2<br />

where<br />

isSpace c = c == ’ ’ || c == ’\t’ || c == ’\n’ || c == ’\r’<br />

-- Concatenate a list of strings with a blank between two strings.<br />

52

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

Saved successfully!

Ooh no, something went wrong!