
Hello everyone, I'm trying to write a generic function which works on binary trees. For example 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) The problem is I couldn't find a way to represent "Subtree" structures. A guy here told me to use variables of type Tree, but I don't know how to create them. Can you tell me how to solve this problem? Thanks a lot.

Hi,
On 11 March 2012 13:36, bahadýr altan
... 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
participants (2)
-
bahadýr altan
-
Ozgur Akgun