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: [Haskell-cafe] Defining a containing function on
      polymorphic list (Andrew Wagner)
   2.  RE: [Haskell-cafe] Defining a containing function on
      polymorphic list (Raeck Zhao)
   3.  Re: [Haskell-cafe] Defining a containing function on
      polymorphic list (Andrew Wagner)
   4.  about the pattern matching (Raeck Zhao)


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

Message: 1
Date: Mon, 22 Dec 2008 09:18:37 -0500
From: "Andrew Wagner" <wagner.and...@gmail.com>
Subject: [Haskell-beginners] Re: [Haskell-cafe] Defining a containing
        function on polymorphic list
To: "Denis Bueno" <dbu...@gmail.com>
Cc: Raeck Zhao <ra...@msn.com>, beginners@haskell.org,
        haskell-c...@haskell.org
Message-ID:
        <b8a8636e0812220618q130e12b1v796d432b7d92b...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Yes, of course, sorry for the typo.

On Mon, Dec 22, 2008 at 9:17 AM, Denis Bueno <dbu...@gmail.com> wrote:

> 2008/12/22 Andrew Wagner <wagner.and...@gmail.com>:
> > The problem here is even slightly deeper than you might realize. For
> > example, what if you have a list of functions. How do you compare two
> > functions to each other to see if they're equal? There is no good way
> really
> > to do it! So, not only is == not completely polymorphic, but it CAN'T be.
> >
> > There is a nice solution for this, however, and it's very simple:
> >
> > contain :: Eq a -> [a] -> Bool
>
> Please note that the syntax here should be:
>
>    contain :: Eq a => a -> [a] -> Bool
>
>                              Denis
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20081222/bf05cd3c/attachment-0001.htm

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

Message: 2
Date: Mon, 22 Dec 2008 14:35:01 +0000
From: Raeck Zhao <ra...@msn.com>
Subject: [Haskell-beginners] RE: [Haskell-cafe] Defining a containing
        function on polymorphic list
To: <wagner.and...@gmail.com>
Cc: Beginners Haskell <beginners@haskell.org>,  Cafe Haskell
        <haskell-c...@haskell.org>
Message-ID: <bay113-w35a83db9659ab95e6c7de5bb...@phx.gbl>
Content-Type: text/plain; charset="iso-8859-1"


Thank you very much for your reply! It is really helpful!

But I just found another 'problem', I just realize that the list does not 
support the user-defined data type?
the list is also depending on the Eq function?

For example,

data Shape = Square | Triangle | Circle

when I type either

[Square, Triangle, Circle]

or 

Square == Square

there are errors!

So there is no way to construct a truly polymorphic List? any way to extend the 
list to support some user-defined data type?

Or...  I define the Shape in a wrong way actually?

Thanks

Raeck


Date: Mon, 22 Dec 2008 09:02:53 -0500
From: wagner.and...@gmail.com
To: ra...@msn.com
Subject: Re: [Haskell-cafe] Defining a containing function on polymorphic list
CC: haskell-c...@haskell.org; beginners@haskell.org

The problem here is even slightly deeper than you might realize. For example, 
what if you have a list of functions. How do you compare two functions to each 
other to see if they're equal? There is no good way really to do it! So, not 
only is == not completely polymorphic, but it CAN'T be. 


There is a nice solution for this, however, and it's very simple:

contain :: Eq a -> [a] -> Bool

contain x [] = False

contain x (y:ys) = if x == y then True else contain x ys

The "Eq a" in the type signature says that 'a' must be a member of the 'Eq' 
typeclass. That says, in turn, that 'a' must have == defined for it. 
Fortunately, most types have, or can easily derive that definition. Here is the 
definition of the typeclass:


class  Eq a  where
(==) :: a -> a -> Bool
(/=) :: a -> a -> Bool

That is, for 'a' to be a member of 'Eq', it must have a == operator which can 
take 2 values of that type and return a Boolean, saying whether or not they're 
equal, and it must also have a definition for the /= operator, which is "not 
equal". These two are also defined in terms of each other, so if you define ==, 
you get /= for free, and vice versa. 


That's probably more information than you needed to know, but I hope it helps.

2008/12/22 Raeck Zhao <ra...@msn.com>









 
I am trying to define a containing function to see if a value is one
of the elements within a list which is polymorphic, but failed with the
following codes:

 > contain :: a -> [a] -> Bool

> contain x [] = False

> contain x (y:ys) = if x == y then True else contain x ys
it seems that the problem is the 'operator' == does not support a polymorphic 
check? 

Any way can solve the problem? or any alternative solution to achieve the 
purpose?

Thanks!

Raeck
 
It's the same Hotmail®. If by "same" you mean up to 70% faster. Get your 
account now.


_______________________________________________

Haskell-Cafe mailing list

haskell-c...@haskell.org

http://www.haskell.org/mailman/listinfo/haskell-cafe




_________________________________________________________________
Life on your PC is safer, easier, and more enjoyable with Windows Vista®. 
http://clk.atdmt.com/MRT/go/127032870/direct/01/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20081222/4762dfff/attachment-0001.htm

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

Message: 3
Date: Mon, 22 Dec 2008 09:44:41 -0500
From: "Andrew Wagner" <wagner.and...@gmail.com>
Subject: [Haskell-beginners] Re: [Haskell-cafe] Defining a containing
        function on polymorphic list
To: "Raeck Zhao" <ra...@msn.com>
Cc: Beginners Haskell <beginners@haskell.org>,  Cafe Haskell
        <haskell-c...@haskell.org>
Message-ID:
        <b8a8636e0812220644u124d5f71s8e58907533df...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

There are two ways to fix this. Let me see if I can get my syntax right this
time :)

1.) Let GHC work out the Eq instance:
  data Shape = Square | Triangle | Circle deriving Eq

2.) Tell GHC how to do it explicitly:
  data Shape = Square | Triangle | Circle
  instance Eq Shape where
    Square == Square = True
    Triangle == Triangle = True
    Circle == Circle = True
    _ == _ = False

Note that the last line here means that any other comparisons are false.

On Mon, Dec 22, 2008 at 9:35 AM, Raeck Zhao <ra...@msn.com> wrote:

>  Thank you very much for your reply! It is really helpful!
>
> But I just found another 'problem', I just realize that the list does not
> support the user-defined data type?
> the list is also depending on the Eq function?
>
> For example,
>
> data Shape = Square | Triangle | Circle
>
> when I type either
>
> [Square, Triangle, Circle]
>
> or
>
> Square == Square
>
> there are errors!
>
> So there is no way to construct a truly polymorphic List? any way to extend
> the list to support some user-defined data type?
>
> Or...  I define the Shape in a wrong way actually?
>
> Thanks
>
> Raeck
>
>
> ------------------------------
> Date: Mon, 22 Dec 2008 09:02:53 -0500
> From: wagner.and...@gmail.com
> To: ra...@msn.com
> Subject: Re: [Haskell-cafe] Defining a containing function on polymorphic
> list
> CC: haskell-c...@haskell.org; beginners@haskell.org
>
> The problem here is even slightly deeper than you might realize. For
> example, what if you have a list of functions. How do you compare two
> functions to each other to see if they're equal? There is no good way really
> to do it! So, not only is == not completely polymorphic, but it CAN'T be.
>
> There is a nice solution for this, however, and it's very simple:
>
> contain :: Eq a -> [a] -> Bool
> contain x [] = False
> contain x (y:ys) = if x == y then True else contain x ys
>
> The "Eq a" in the type signature says that 'a' must be a member of the 'Eq'
> typeclass. That says, in turn, that 'a' must have == defined for it.
> Fortunately, most types have, or can easily derive that definition. Here is
> the definition of the typeclass:
>
> class 
> Eq<http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Eq.html#t:Eq>a
> where 
> (==)<http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Eq.html#v:%3D%3D>::
>  a -> a ->
> Bool<http://haskell.org/ghc/docs/latest/html/libraries/ghc-prim/GHC-Bool.html#t:Bool>
> (/=)<http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Eq.html#v:/%3D>::
>  a -> a ->
> Bool<http://haskell.org/ghc/docs/latest/html/libraries/ghc-prim/GHC-Bool.html#t:Bool>
> That is, for 'a' to be a member of 'Eq', it must have a == operator which
> can take 2 values of that type and return a Boolean, saying whether or not
> they're equal, and it must also have a definition for the /= operator, which
> is "not equal". These two are also defined in terms of each other, so if you
> define ==, you get /= for free, and vice versa.
>
> That's probably more information than you needed to know, but I hope it
> helps.
>
> 2008/12/22 Raeck Zhao <ra...@msn.com>
>
>  I am trying to define a containing function to see if a value is one of
> the elements within a list which is polymorphic, but failed with the
> following codes:
> > contain :: a -> [a] -> Bool
> > contain x [] = False
> > contain x (y:ys) = if x == y then True else contain x ys it seems that
> the problem is the 'operator' == does not support a polymorphic check?
> Any way can solve the problem? or any alternative solution to achieve the
> purpose?
> Thanks!
> Raeck
>
> ------------------------------
> It's the same Hotmail(R). If by "same" you mean up to 70% faster. Get your
> account 
> now.<http://windowslive.com/online/hotmail?ocid=TXT_TAGLM_WL_hotmail_acq_broad1_122008>
>
> _______________________________________________
> Haskell-Cafe mailing list
> haskell-c...@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
>
> ------------------------------
> Life on your PC is safer, easier, and more enjoyable with Windows Vista(R). 
> See
> how <http://clk.atdmt.com/MRT/go/127032870/direct/01/>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20081222/9c4469da/attachment-0001.htm

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

Message: 4
Date: Wed, 24 Dec 2008 01:45:05 +0000
From: Raeck Zhao <ra...@msn.com>
Subject: [Haskell-beginners] about the pattern matching
To: Beginners Haskell <beginners@haskell.org>
Message-ID: <bay113-w412aec01f35cdb734d7f51bb...@phx.gbl>
Content-Type: text/plain; charset="iso-8859-1"


hi, good ... morning : )
I am just confused by the following code

> oneOnly :: [Int]
> oneOnly = [1]
> isOneOnly :: [Int] -> Bool
> isOneOnly oneOnly = True
> isOneOnly tester = False

what I want to do is to define a 'type' oneOnly as [1] and use it on 
the pattern matching in function isOneOnly. But it does not work as
I expect:

When I type

isOneOnly [1] 

it will be  True  which is the result I expect but for

is OneOnly [1,2]

the result keeps True, it seems the second pattern has been ignored,
I think I try to achieve the purpose in a wrong way, any suggestion?

Thanks and Merry Christmas

Best wishes,
Raeck

_________________________________________________________________
Send e-mail anywhere. No map, no compass.
http://windowslive.com/oneline/hotmail?ocid=TXT_TAGLM_WL_hotmail_acq_anywhere_122008
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20081224/c0486455/attachment.htm

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

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://www.haskell.org/mailman/listinfo/beginners


End of Beginners Digest, Vol 6, Issue 7
***************************************

Reply via email to