[Haskell-cafe] Simple "Maybe" question

Mark Carroll mark at chaos.x-philes.com
Tue Jan 27 08:15:57 EST 2004


On Tue, 27 Jan 2004, Jim Lewis wrote:

> I'm new to Haskell and can't find an example to solve a trivial problem.
>
> I have code like this:
> findBlank :: [] -> Int
> findBlank str = findIndex (==' ') str
>
> But interpreter complains elsewhere of mismatch of Int with Maybe Int. I want to handle the maybe only here and nowhere else.
(snip)

This may somehow help,

findBlank' :: [Char] -> Int
findBlank' str = fromJust (findIndex (==' ') str)

findBlank'' :: [Char] -> Int
findBlank'' str = let Just index = findIndex (==' ') str in index

findBlank''' :: [Char] -> Int
findBlank''' str = maybe (error "Pigs flew") id (findIndex (==' ') str)

You may have to import Maybe to get some of those to work.

Matthew Walton's comments also look good.

-- Mark


More information about the Haskell-Cafe mailing list