[Haskell-beginners] mayBe stuck

aditya siram aditya.siram at gmail.com
Thu Aug 5 18:42:40 EDT 2010


The problem was the use of 'i' in 'take' and drop both of which require a
Int. Here is code that corrects that:

import Data.List
import Data.Maybe

eqD = (=='%')
test4 = let s = "zaoeu%aeuasnt%staashaeu%nthdanoe%nthd" in
  putStrLn $ show $ brS (findIndex eqD s) s

brS :: Maybe Int -> String -> String
brS i ss
   | isNothing i   = ss
   | otherwise     = (take (fromJust i) ss) ++ (brS newIndex newStr)
                       where
                           newIndex    = findIndex eqD newStr
                           newStr      = drop ((fromJust i) +1) ss


-deech

On Thu, Aug 5, 2010 at 5:32 PM, aditya siram <aditya.siram at gmail.com> wrote:

> Sorry didn't read that properly. Hold on.
> -deech
>
>
> On Thu, Aug 5, 2010 at 5:31 PM, aditya siram <aditya.siram at gmail.com>wrote:
>
>> This is happening because findIndex has the signature:
>> findIndex<http://hackage.haskell.org/packages/archive/haskell98/latest/doc/html/List.html#v:findIndex>:: (a -> Bool) -> [a] -> Maybe Int
>>
>> From this we know that 'findIndex' can return a 'Just Int' or 'Nothing'.
>>
>> GHC is telling you that you need to handle the case where the list element
>> you ask for does not exist and findIndex returns 'Nothing'.
>>
>> The functions in the Maybe module [1]may be of some help here.
>>
>> -deech
>>
>> [1]
>> http://hackage.haskell.org/packages/archive/haskell98/latest/doc/html/Maybe.html
>>
>>
>> On Thu, Aug 5, 2010 at 5:22 PM, prad <prad at towardsfreedom.com> 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)
>>>                        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.
>>>
>>> however, i'm not sure how to resolve what seems to me to be an issue
>>> between a Maybe and an Int.
>>>
>>>
>>>
>>> --
>>> In friendship,
>>> prad
>>>
>>>                                      ... with you on your journey
>>> Towards Freedom
>>> http://www.towardsfreedom.com (website)
>>> Information, Inspiration, Imagination - truly a site for soaring I's
>>> _______________________________________________
>>> Beginners mailing list
>>> Beginners at haskell.org
>>> http://www.haskell.org/mailman/listinfo/beginners
>>>
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/beginners/attachments/20100805/17d64cf5/attachment.html


More information about the Beginners mailing list