Send Beginners mailing list submissions to
        [email protected]

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
        [email protected]

You can reach the person managing the list at
        [email protected]

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


Today's Topics:

   1. Re:  about the pattern matching (Andrew Wagner)
   2. Re:  about the pattern matching (Daniel Fischer)
   3.  Re: [Haskell-cafe] Defining a containing function on
      polymorphic list (Denis Bueno)
   4.  Re: [Haskell-cafe] Defining a containing function on
      polymorphic list (Luke Palmer)
   5.  Re: [Haskell-cafe] Defining a containing function on
      polymorphic list (Thomas Davie)
   6.  Re: [Haskell-cafe] Defining a containing function on
      polymorphic list (Ryan Ingram)


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

Message: 1
Date: Tue, 23 Dec 2008 20:30:45 -0600
From: "Andrew Wagner" <[email protected]>
Subject: Re: [Haskell-beginners] about the pattern matching
To: "Raeck Zhao" <[email protected]>
Cc: Beginners Haskell <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

The line "isOneOnly oneOnly = True" doesn't do what you expect here.
Basically, it says there are no constraints on the input, and it binds
whatever input it gets to a new *local* variable named oneOnly. Thus, it
always matches that line of the function, and always returns true.

The problem here is that [1] is a value, not a type that you can match on.
If you want to make sure the value is [1], you can do it one of these two
ways:
isOneOnly x = x == [1]

or

isOneOnly x | x == [1] = True
isOneOnly x | otherwise = False

Now at this point you may be wondering how functions like this work:
factorial 1 = 1
factorial n = n * factorial (n-1)

I just said that you can't match against values, but against types. What
gives? Well, in fact, haskell matches against integers as types of sort, of
the structure Succ Int. That is, it treats integers like they were encoded
with the following type:

data Nat = Zero | Succ Nat

Now you can see how it could pattern match against integers, just like it
would against other types with data constructors. Anyway, I"m sure this is
far more information than you wanted to know.

On Tue, Dec 23, 2008 at 7:45 PM, Raeck Zhao <[email protected]> wrote:

>  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. Get your Hotmail(R) account 
> now.<http://windowslive.com/oneline/hotmail?ocid=TXT_TAGLM_WL_hotmail_acq_anywhere_122008>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20081223/7c7ff8f0/attachment-0001.htm

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

Message: 2
Date: Wed, 24 Dec 2008 03:39:52 +0100
From: Daniel Fischer <[email protected]>
Subject: Re: [Haskell-beginners] about the pattern matching
To: Raeck Zhao <[email protected]>, Beginners Haskell
        <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain;  charset="iso-8859-1"

Am Mittwoch, 24. Dezember 2008 02:45 schrieb Raeck Zhao:
> 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?

In "isOneOnly oneOnly ", the pattern oneOnly is a variable pattern, it matches 
everything, it also matches [] and _|_. the fact that it is also the name of 
an entity defined elsewhere doesn't matter. You can find more about pattern 
matching (basically, a pattern is a wildcard, a variable or a constructor 
applied to patterns) in the report.
If you turn on warnings for overlapping patterns, GHC will warn you:

$ ghci -fwarn-overlapping-patterns OneOnly
GHCi, version 6.8.3: http://www.haskell.org/ghc/  :? for help
Loading package base ... linking ... done.
[1 of 1] Compiling Main             ( OneOnly.hs, interpreted )

OneOnly.hs:4:0:
    Warning: Pattern match(es) are overlapped
             In the definition of `isOneOnly': isOneOnly tester = ...
Ok, modules loaded: Main.
*Main> isOneOnly undefined
True


Depending on what you want to achieve, maybe 
isOneOnly [1] = True
isOneOnly _ = False

or
isOneOnly [_] = True
isOneOnly _ = False

is the solution.
>
> Thanks and Merry Christmas
>
> Best wishes,
> Raeck
>



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

Message: 3
Date: Mon, 22 Dec 2008 07:17:25 -0700
From: "Denis Bueno" <[email protected]>
Subject: [Haskell-beginners] Re: [Haskell-cafe] Defining a containing
        function on polymorphic list
To: "Andrew Wagner" <[email protected]>
Cc: Raeck Zhao <[email protected]>, [email protected],
        [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

2008/12/22 Andrew Wagner <[email protected]>:
> 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


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

Message: 4
Date: Mon, 22 Dec 2008 12:01:09 -0700
From: "Luke Palmer" <[email protected]>
Subject: [Haskell-beginners] Re: [Haskell-cafe] Defining a containing
        function on polymorphic list
To: "Raeck Zhao" <[email protected]>
Cc: Beginners Haskell <[email protected]>,  Cafe Haskell
        <[email protected]>
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

2008/12/22 Raeck Zhao <[email protected]>

>  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]
>

This is perfectly legal, but GHCi won't be able to print it, because there
is no Show instance for Shape.  You can declare one:

instance Show Shape where
    show Square = "Square"
    show Triagle = "Triangle"
    show Circle = "Circle"

This can be generated automatically when you declare the type, by using:

data Shape = Square | Triangle | Circle
    deriving (Show)


>
>
> or
>
> Square == Square
>

Similarly, to use (==), you need an Eq instance, which can be defined much
in the same way as the Show instance above  (deriving also works on Eq --
don't generalize too hastily; not all classes work with deriving).

Luke
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20081222/d91c3e78/attachment-0001.htm

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

Message: 5
Date: Mon, 22 Dec 2008 15:31:35 +0100
From: Thomas Davie <[email protected]>
Subject: [Haskell-beginners] Re: [Haskell-cafe] Defining a containing
        function on polymorphic list
To: "Andrew Wagner" <[email protected]>
Cc: Raeck Zhao <[email protected]>, [email protected],
        [email protected],       Denis Bueno <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"


On 22 Dec 2008, at 15:18, Andrew Wagner wrote:

> Yes, of course, sorry for the typo.
>
> On Mon, Dec 22, 2008 at 9:17 AM, Denis Bueno <[email protected]> wrote:
> 2008/12/22 Andrew Wagner <[email protected]>:
> > 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

Of note, unless this is an exercise, such a function already exists --  
it's called elem.

How do you find such a function?  You search on haskell.org/hoogle.

http://haskell.org/hoogle/?hoogle=Eq+a+%3D%3E+a+-%3E+%5Ba%5D+-%3E+Bool

Bob
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20081222/ad44253f/attachment-0001.htm

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

Message: 6
Date: Wed, 24 Dec 2008 07:07:14 -0800
From: "Ryan Ingram" <[email protected]>
Subject: [Haskell-beginners] Re: [Haskell-cafe] Defining a containing
        function on polymorphic list
To: "Raeck Zhao" <[email protected]>
Cc: [email protected], [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

Here's a tip: leave off the type signature, and ask ghci what it is.

$ ghci
Prelude> let contain x [] = False ; contain x (y:ys) = if x == y then
True else contain x ys
Prelude> :t contain
contain :: (Eq a) => a -> [a] -> Bool

  -- ryan

2008/12/22 Raeck Zhao <[email protected]>:
> 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.
> _______________________________________________
> Haskell-Cafe mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>


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

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


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

Reply via email to