[clean-list] constant Tree as pattern ?

G.W.M. Vissers vissers@theochem.kun.nl
Mon, 31 Mar 2003 15:26:04 +0200 (CEST)


Hello Koen,

are you sure that the program as you sent it compiles? Using Clean 2.0.2 
on a Linux pc the compiler rightly complains about two things:

Parse error [tmp.icl,3;13,Algebraic type: constructor definitions]: 
constructor name expected instead of nil

and after changing the definition to 

:: Tree a = Nil | Node a (Tree a) (Tree a)

it complains about the missing argument in the "test" function:

Error [tmp.icl,8,test]: This alternative has 0 arguments instead of 1.

Fixing that:


module tmp

:: Tree a = Nil | Node a (Tree a) (Tree a)

test :: (Tree Int) -> Int
test (Node 1 Nil Nil) = 1
test (Node 2 Nil Nil) = 2
test _ = 3

Start = test (Node 2 Nil Nil)

 
This works for me (i.e. gives 2). 

regards,

Gé 

> Hello,
> 
> I am a newcomer to Clean, and have the following question (Clean 2.0.2)
> about the following piece of code:
> 
> :: Tree a = nil | Node a (Tree a) (Tree a)
> 
> test :: (Tree Int) -> Int
> test (Node 1 nil nil) = 1
> test (Node 2 nil nil) = 2
> test = 3
> 
> Start = test (Node 1 nil nil)
> 
> compiles fine and gives 1 as the result of the Start function
> 
> When i replace Start with
> 
> Start = test (Node 2 nil nil)
> 
> however, i still get 1 as the result of Start when I expext 2 as the result.
> 
> >From the language report (chapter 3.2) I understand I can use any kind of
> constant as a pattern. In my understanding of Clean at this moment the
> expression (Node 2 nil nil) is a constant of type (Tree Int).
> 
> What is the flaw in my thinking ?
> 
> Koen van Dijken
> 
> 
> 
> 
> _______________________________________________
> clean-list mailing list
> clean-list@cs.kun.nl
> http://www.cs.kun.nl/mailman/listinfo/clean-list
>