[Haskell-cafe] Head and tail matching question
Jules Bean
jules at jellybean.co.uk
Mon Jun 11 12:45:25 EDT 2007
Olivier Boudry wrote:
> Hi all,
>
> I'm trying to write a untab function that would split a string on tabs
> and return a list. Code is here.
>
> import Data.List (break, unfoldr)
> import Data.Char (String)
>
> untab :: String -> [String]
> untab s = unfoldr untab' s
>
> untab' :: String -> Maybe (String, String)
> untab' s | s == "" = Nothing
> | otherwise = Just (h, ts)
> where (h, t:ts) = break (== '\t') s
>
> This code raises an exception when handling the last portion of the
> string. Break returns a ("something", "") and t:ts cannot match on "".
untab' [] = Nothing
untab' s = Just (h , drop 1 t)
where (h,t) = break (== '\t') s
More information about the Haskell-Cafe
mailing list