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.  pattern matching on date type (Max.cs)
   2. Re:  pattern matching on date type (Alexander Dunlap)
   3.  Re: about the concatenation on a tree (Thomas Davie)
   4. Re:  pattern matching on date type (Thomas Davie)
   5. Re:  until and Time (Steve Klabnik)
   6.  Question about typeclasses (keke)
   7. Re:  Question about typeclasses (Jason Dusek)


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

Message: 1
Date: Thu, 1 Jan 2009 08:36:24 -0000
From: "Max.cs" <[email protected]>
Subject: [Haskell-beginners] pattern matching on date type
To: "Brandon S. Allbery KF8NH" <[email protected]>,
        <[email protected]>, <[email protected]>
Message-ID: <3d524ba96876446ca9b57bdb8e096...@hxzhao>
Content-Type: text/plain; charset="iso-8859-1"

thanks!

suppose we have 

> data Tree a = Leaf a | Branch (Tree a) (Tree a) deriving Show

and how I could define a function foo :: a -> Tree a that

foo a = Leaf a         where a is not a type of Tree
foo b = b                 where b is one of the type of Tree (Leaf or Branch) ?

The following code seems not working......

foo (Leaf a) = a
foo a = Leaf a

saying 'Couldn't match expected type `a' against inferred type `Btree a''

any idea?

Thanks,
Max



From: Brandon S. Allbery KF8NH 
Sent: Thursday, January 01, 2009 7:35 AM
To: Max.cs 
Cc: Brandon S. Allbery KF8NH ; [email protected] ; [email protected] 
Subject: Re: [Haskell-cafe] definition of data


On 2009 Jan 1, at 2:32, Max.cs wrote: 
  data Tree a = a | Branch (Tree a) (Tree a) deriving Show

  but it seems not accpetable in haskell ?


You need a constructor in both legs of the type:


> data Tree a = Leaf a | Branch (Tree a) (Tree a) deriving Show


-- 
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] [email protected]
system administrator [openafs,heimdal,too many hats] [email protected]
electrical and computer engineering, carnegie mellon university    KF8NH


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

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

Message: 2
Date: Thu, 1 Jan 2009 01:44:33 -0800
From: "Alexander Dunlap" <[email protected]>
Subject: Re: [Haskell-beginners] pattern matching on date type
To: Max.cs <[email protected]>
Cc: [email protected], [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1

On Thu, Jan 1, 2009 at 12:36 AM, Max.cs <[email protected]> wrote:
> thanks!
>
> suppose we have
>
>> data Tree a = Leaf a | Branch (Tree a) (Tree a) deriving Show
>
> and how I could define a function foo :: a -> Tree a that
>
> foo a = Leaf a         where a is not a type of Tree
> foo b = b                 where b is one of the type of Tree (Leaf or
> Branch) ?
>
> The following code seems not working......
>
> foo (Leaf a) = a
> foo a = Leaf a
>
> saying 'Couldn't match expected type `a' against inferred type `Btree a''
>
> any idea?
>
> Thanks,
> Max

You can't define such a function. foo :: a -> Tree a, but the
definition foo b = b has the type a -> a, which is why the compiler
says it can't match type "a" against "Tree a". In general, Haskell
functions can't "look" at the type of their arguments: they are either
monomorphic or are polymorphic, in which case you can only use
polymorphic functions that match the polymorphic type. For the problem
you are trying to solve, you probably need to encode this logic higher
up in the overall function (e.g. have one function to deal with "a"s
and another to deal with "Tree a"s).

Hope that helps,
Alex


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

Message: 3
Date: Thu, 1 Jan 2009 10:58:26 +0100
From: Thomas Davie <[email protected]>
Subject: [Haskell-beginners] Re: about the concatenation on a tree
To: "Max.cs" <[email protected]>
Cc: Beginners Haskell <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=WINDOWS-1252; format=flowed;
        delsp=yes

>
> hi Bob,
>
> I believe the type of foldTree is implementable, but what confuses  
> me is
>
> the output example:
>
>> concatT (Leaf t) = t
>> concatT (Branch (Leaf t1) (Leaf t2)) = Branch t1 t2
>
> they seem to be inconsistent with the type of concatT which return a  
> Tree only, but in the Leaf case, a atomic value is returned?
>
> anything important I am missing?

You need to remember what type is stored in that Leaf – remember that  
this is a Tree (Tree a) – i.e. the variable t in the line above has  
the type Tree a.

Bob

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

Message: 4
Date: Thu, 1 Jan 2009 11:04:23 +0100
From: Thomas Davie <[email protected]>
Subject: Re: [Haskell-beginners] pattern matching on date type
To: "Max.cs" <[email protected]>
Cc: [email protected], [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=WINDOWS-1252; format=flowed;
        delsp=yes


On 1 Jan 2009, at 09:36, Max.cs wrote:

> thanks!
>
> suppose we have
>
> > data Tree a = Leaf a | Branch (Tree a) (Tree a) deriving Show
>
> and how I could define a function foo :: a -> Tree a that
>
> foo a = Leaf a         where a is not a type of Tree
> foo b = b                 where b is one of the type of Tree (Leaf  
> or Branch) ?
>
> The following code seems not working......
>
> foo (Leaf a) = a
> foo a = Leaf a
>
> saying 'Couldn't match expected type `a' against inferred type  
> `Btree a'
Hi again Max,
   I'm assuming this is continuing from the concatT example, and that  
you're struggling with first function you must pass to foldTree.   
Remember the type of the function – it's not a -> Tree a, but Tree a - 
 > Tree a, because your leaves in the parent tree all contain trees to  
glue on at that point.

So, the function you want, is the function which looks at the  
parameter it's given, goes "oh, that's interesting", does nothing to  
it, and hands it back to replace the Leaf.   I recommend searching  
hoogle for functions of type a -> a, the function you're looking for  
is built in.

Bob

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

Message: 5
Date: Thu, 1 Jan 2009 12:28:00 -0800
From: "Steve Klabnik" <[email protected]>
Subject: Re: [Haskell-beginners] until and Time
To: "Daniel Fischer" <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"

Ahh. Thank you. That makes much more sense now.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20090101/29d59a85/attachment-0001.htm

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

Message: 6
Date: Sat, 3 Jan 2009 16:17:17 +0800
From: keke <[email protected]>
Subject: [Haskell-beginners] Question about typeclasses
To: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset="utf-8"

Hi,

I am bit struggling with understanding the typeclasses usage in
Text.Regex.Posix (=~) while reading Real world haskell.

The type of (=~) is (RegexMaker Regex CompOption ExecOption source,
RegexContext Regex source1 target)  => source1 -> source -> target

I am from Java world. My question is that can I understand source1 in the
way which is a value has type of RegexMaker, Regex, CompOption and
ExecOption?

And the definiton of instance RegexMaker Regex CompOption ExecOption
Stringmakes it possible for us to pass a String as the parameter of
=~?

Where can I find some good metarials about GHC's type classes? I googled a
lot but can not find something mentioned above usage.

-- 
Cheers,
Keke
-----------------
We paranoid love life
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
http://www.haskell.org/pipermail/beginners/attachments/20090103/76e53996/attachment-0001.htm

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

Message: 7
Date: Sat, 3 Jan 2009 01:26:23 -0800
From: "Jason Dusek" <[email protected]>
Subject: Re: [Haskell-beginners] Question about typeclasses
To: keke <[email protected]>
Cc: [email protected]
Message-ID:
        <[email protected]>
Content-Type: text/plain; charset=UTF-8

<[email protected]>                         2009-01-03T08:17:00Z
> I am bit struggling with understanding the typeclasses usage
> in Text.Regex.Posix (=~) [...] My question is that can I
> understand source1 in the way which is a value has type of
> RegexMaker, Regex, CompOption and ExecOption?

  Prettifying the signature a little bit:

   :: ( RegexMaker Regex CompOption ExecOption source
      , RegexContext Regex source1 target )
   => source1
   -> source
   -> target

  You can read this as:

    The type `source1 -> source -> target` such that both "class
    constraints" are satisfied.

  What is a class contraint and what does it mean for it to be
  satisfied? A class contraint is something like an interface,
  or a generic, or a template. One defines "instances" of
  classes in Haskell, as in Jave one "implements" an interface.
  So what do these particular classes mean? The first one reads:

    We have a way to make a regular expression using the usual
    option specifications (multiline, case insensitive, stuff
    like that -- those are `CompOption` and `ExecOption`).

  The second one reads

    There is a way to match the first value against the second
    to get the desired target value (list of matches, Boolean,
    &c.).

  The "way to..." in each case is a function in the class
  definition -- `makeRegex` in the former case, and `match` in
  the latter.

--
Jason Dusek


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

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


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

Reply via email to