User-Defined Types

Ketil Z. Malde ketil@ii.uib.no
01 May 2002 17:18:39 +0200


Matthias <matthias.pfeifer@post.rwth-aachen.de> writes:

> "Type Constructors and data Constructors are in separate
> namespaces. [example]" 

> But according to the chapter (2.2.1) "Recursive Types" i see the
> polymorphic definition of a tree is:

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

> where (Tree a) is obviously the data-constructor out of "the other
> namespace". I guess that the ()

The parentheses are only for delimiting, and I think you're making
this more complex than it really is.  Read the declaration as

        "a Tree of 'a's (or perhaps "over 'a'"?) is either a Leaf
        containing an 'a', or a Branch containing two (sub)Trees of
        'a's (over 'a')."

> Also i would not regret some hints about "->" which
> is used in function-type-definitions. I would prefer writing

> 	plus :: a,a -> a 

Feel free, only in order to make it a tuple, you need the parentheses:

        plus :: (a,a) -> a

is a perfectly valid declaration.  But keep in mind that

        plus :: a -> a -> a

is really a function that takes an 'a' and returns another function

        plus' :: a -> a

that can take an 'a' and return an 'a'.  So, with the usual + operator
defined like this, we can do cool stuff like

        increment = (+1)

and do 
        
        increment 4
        => 5
        map increment [1,2,3]
        => [2,3,4]

-kzm
-- 
If I haven't seen further, it is by standing in the footprints of giants