[Haskell-cafe] Head and tail matching question

Olivier Boudry olivier.boudry at gmail.com
Mon Jun 11 11:26:42 EDT 2007

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 "".

I was wondering if there way a clean way of handling this last case
without adding tons of code. Some kind of "idiomatic expression" ;-)



More information about the Haskell-Cafe mailing list