[Haskell-beginners] mayBe stuck
byorgey at seas.upenn.edu
Thu Aug 5 18:39:23 EDT 2010
On Thu, Aug 05, 2010 at 03:22:35PM -0700, prad wrote:
> 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)
> newIndex = findIndex eqD newStr
> newStr = drop (i+1) ss
Because of the (i+1) argument to drop, GHC infers that i must be an
Int. Because i is used as an argument to isNothing, GHC infers that i
must have the type (Maybe a) for some type a. It cannot be both.
If I were you I would define brS by pattern matching, like so:
brS Nothing ss = ss
brS (Just i) ss = ...
Now in the ... i really will be an Int.
Also, did you know there is already code to do this in the 'split'
package on Hackage? (Just 'cabal install split' and look at the
'Data.List.Split' module.) But if you're just writing this function in
order to learn, then no problem.
More information about the Beginners