Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trees in Haskell

Tags:

Still learning haskell, and I cannot really see the difference between

data Tree a = Leaf a | Branch [Tree a] 

and

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

What is best according to you? What is the implication of these two ways of writing ?

like image 655
Stephane Rolland Avatar asked Dec 13 '10 17:12

Stephane Rolland


People also ask

What is a node in Haskell?

Node is a recursive constructor. It has a value of type a that it receives as a parameter on construction. The second parameter to Node is of type Seq a which is the type of Node itself, thus it becomes a recursive data structure.

What is a zipper Haskell?

From HaskellWiki. The Zipper is an idiom that uses the idea of “context” to the means of manipulating locations in a data structure. Zipper monad is a monad which implements the zipper for binary trees.


1 Answers

The branch of the first one holds a list of Trees, so potentially any number of subtrees. The 2nd is explicitly two subtrees, thus a binary tree.

like image 53
Tyler Eaves Avatar answered Nov 16 '22 01:11

Tyler Eaves