Send Beginners mailing list submissions to
        beginners@haskell.org

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  mayBe stuck (Brent Yorgey)
   2. Re:  mayBe stuck (aditya siram)
   3. Re:  mayBe stuck (Brent Yorgey)
   4. Re:  mayBe stuck (J?rgen Doser)
   5. Re:  mayBe stuck (aditya siram)
   6.  Re: mayBe stuck (prad)


----------------------------------------------------------------------

Message: 1
Date: Thu, 5 Aug 2010 23:39:23 +0100
From: Brent Yorgey <byor...@seas.upenn.edu>
Subject: Re: [Haskell-beginners] mayBe stuck
To: beginners@haskell.org
Message-ID: <20100805223923.ga15...@seas.upenn.edu>
Content-Type: text/plain; charset=us-ascii

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)
>                         where
>                             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.

-Brent


------------------------------

Message: 2
Date: Thu, 5 Aug 2010 17:42:40 -0500
From: aditya siram <aditya.si...@gmail.com>
Subject: Re: [Haskell-beginners] mayBe stuck
To: prad <p...@towardsfreedom.com>
Cc: haskellbeginners <beginners@haskell.org>
Message-ID:
        <aanlkti=-zovasooxcgg6sh838y1pgktskgwos6r4k...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

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.si...@gmail.com> wrote:

> Sorry didn't read that properly. Hold on.
> -deech
>
>
> On Thu, Aug 5, 2010 at 5:31 PM, aditya siram <aditya.si...@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 <p...@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@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-0001.html

------------------------------

Message: 3
Date: Thu, 5 Aug 2010 23:47:52 +0100
From: Brent Yorgey <byor...@seas.upenn.edu>
Subject: Re: [Haskell-beginners] mayBe stuck
To: beginners@haskell.org
Message-ID: <20100805224752.gb15...@seas.upenn.edu>
Content-Type: text/plain; charset=us-ascii

On Thu, Aug 05, 2010 at 05:42:40PM -0500, aditya siram wrote:
> 
> 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

/me makes deech write on the blackboard 100 times, "I will not use fromJust"


------------------------------

Message: 4
Date: Fri, 06 Aug 2010 00:52:51 +0200
From: J?rgen Doser <jurgen.do...@gmail.com>
Subject: Re: [Haskell-beginners] mayBe stuck
To: beginners@haskell.org
Message-ID: <1281048771.3497.8.ca...@imedia.irun.org>
Content-Type: text/plain; charset=utf-8

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.

now:
                           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
newIndex.

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?

        Jürgen



------------------------------

Message: 5
Date: Thu, 5 Aug 2010 18:20:06 -0500
From: aditya siram <aditya.si...@gmail.com>
Subject: Re: [Haskell-beginners] mayBe stuck
To: Brent Yorgey <byor...@seas.upenn.edu>
Cc: beginners@haskell.org
Message-ID:
        <aanlkti=eis9g8v1j-zrigu3wudyb+1ycp8kbidd8s...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Normally yes, but here we are guaranteed to get a 'Just ...' value because
of the 'isNothing' guard.
-deech

On Thu, Aug 5, 2010 at 5:47 PM, Brent Yorgey <byor...@seas.upenn.edu> wrote:

> On Thu, Aug 05, 2010 at 05:42:40PM -0500, aditya siram wrote:
> >
> > 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
>
> /me makes deech write on the blackboard 100 times, "I will not use
> fromJust"
> _______________________________________________
> Beginners mailing list
> Beginners@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/de6df37c/attachment-0001.html

------------------------------

Message: 6
Date: Thu, 5 Aug 2010 17:00:13 -0700
From: prad <p...@towardsfreedom.com>
Subject: [Haskell-beginners] Re: mayBe stuck
To: beginners@haskell.org
Message-ID: <20100805170013.61eeb...@gom>
Content-Type: text/plain; charset=US-ASCII

On Thu, 5 Aug 2010 23:39:23 +0100
Brent Yorgey <byor...@seas.upenn.edu> wrote:

> 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.) 
>
thx brent. i've looked at various goodies from cabal and am trying to
understand the code. for instance, there is a split function in Useful
that i was trying to figure out as well as a replace (which is what
this is eventually going to develop into). i'm just trying to see if i
can get a better understanding by coming up with my own versions as
well.

unfortunately, i had no clue about the issue with mayBe and how to deal
with findIndex. though this Just Int and Nothing business is an
inconvenience presently, it feels somehow more honest that what some
languages do where if something isn't found the function returns 0.

thank you for your explanation too as well as those by deech and
jurgen. they've cleared up several things.



-- 
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@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 26, Issue 12
*****************************************

Reply via email to