[Haskell-beginners] Type Tree variables
Ozgur Akgun
ozgurakgun at gmail.com
Sun Mar 11 15:05:38 CET 2012
Hi,
On 11 March 2012 13:36, bahadýr altan <doaltan at yahoo.co.uk> wrote:
> ... my function works on just first three nodes of a binary tree, but
> the tree is bigger, I don't care the other nodes. Like this :
>
> function Node a (Node b Subtree Subtree) (Node c Subtree Subtree)
>
Assuming you use the following declaration for your data type:
data Tree a = Leaf | Node a (Tree a) (Tree a)
The syntax you are looking for is the following; where x, y and z are
pattern variables.
f :: Tree a -> (a,a,a)
f (Node x (Node y _ _) (Node z _ _)) = (x,y,z)
The above function is obviously partial. It would throw an exception if you
apply it to anything which doesn't have the 'proper' shape. (e.g. "f Leaf")
I don't know your background, but I'd suggest reading some introductory
material if you are having trouble understanding the above snippets. LYAH
is a pretty fun read: http://learnyouahaskell.com
Hope this helps,
Ozgur
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20120311/00068ae4/attachment.htm>
More information about the Beginners
mailing list