[Haskell-beginners] mayBe stuck

Jürgen Doser jurgen.doser at gmail.com
Thu Aug 5 18:52:51 EDT 2010

El jue, 05-08-2010 a las 15:22 -0700, prad escribió:
> i'm trying to create my own split function with % as delimiter.
> so i have
> eqD = (=='%')
> and send 
>     let s = "zaoeu%aeuasnt%staashaeu%nthdanoe%nthd"
>     putStrLn $ show $ brS (findIndex eqD s) s 
> to a function brS:
> brS i ss
>     | isNothing i   = ss
>     | otherwise     = (take i ss) : (brS newIndex newStr)
>                         where
>                             newIndex    = findIndex eqD newStr
>                             newStr      = drop (i+1) ss
> but get the following error:
>     Couldn't match expected type `Maybe a' against inferred type `Int'
>     In the first argument of `isNothing', namely `i'
>     In the expression: isNothing i :: mayBe a
>     In a stmt of a pattern guard for
>                  the definition of `brS':
>           isNothing i :: mayBe a
> my understanding is that i need the isNothing because findIndex will
> return Just Int or Nothing.

Yes. But ghc is telling you that 'i' should have type Maybe a, whereas
it has the type Int.

look at the following line:

    | otherwise     = (take i ss) : (brS newIndex newStr)

the 'take i ss' tells ghc that i is an Int. the 'brs newIndex newStr'
tells ghc that i has the same type as newIndex.

                           newIndex    = findIndex eqD newStr

this tells ghc that newIndex has type Maybe Int, which does not match
the type Int inferred above. You would have to fix the definition of

But really, you are doing it the wrong way. Your code is traversing the
list once to find sth. (the findIndex ...), and then traversing it again
to split it (the take i ...). Why not split directly when you find what
you are looking for?


More information about the Beginners mailing list