Trees in Haskell
Solution 1
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.
Solution 2
The former defines a tree where each branch can have arbitrarily many subtrees (represented as a list of trees) and the latter defines a tree where each branch has exactly two subtrees.
In other words the former is a general tree and the latter is a binary tree.
So which one to choose depends on whether you want to model a general tree or a binary tree.
Solution 3
I've put this as an answer rather than comment so it have some formatting:
data Rose a = Branch a [Rose a]
deriving (Show)
sample1 :: Rose Int
sample1 = Branch 1 [Branch 2 [], Branch 3 [Branch 5 []], Branch 4 []]
This is the same as the library module Data.Tree, although Data.Tree uses field-labels and a type synonym.
I've seen both this tree and your first definition called "Rose trees" although they have slightly different shapes so the terminology doesn't seem to be entirely precise. My interpretation is that it is the list "[Rose a]" embedded in the single recursive constructor that is defining it as a Rose tree.

Stephane Rolland
Author of the PL XYZ (name is still to be defined, also I'm extremely late compared to what I was expecting, and it's quite possible there's at least one year of work before I can release something). At marathon my time is 5:30, that's far from a world record. I plan longer trails (60 km seems feasible for now) after I have made public the version 0.0 of the language. Machine Learning: kaggle AI competitions Favourite Techs: Haskell, C++14, Python 3, PostgreSQL, NixOs Linux, Zsh/Bash/Perl, XMonad. Photography/Graphical Arts: Abstract Photography , Pinterests Boards Author-Composer: Publicly released music, tracks on soundcloud Interests: Language Design, Categorial and Linear logic, Type Theory, Artificial Intelligence, Design Patterns, Lambda Calculus, Web Design, Pi Calculus, Pattern Calculus, Digital Signal Processing, Test Driven Development, Software Craftsmanship Everyday experience: C++, Python, Haskell Practicing: Perl, Cython/CPython, Machine Learning, Scikit-Learn Used: C, C#, Xml, Regex, Sql, Javascript, Win32, Mfc, Com/Atl, Stl, Design Patterns, MsXml, Wpf, C++/Cli, Html, Java, Soap, Sql Server, Http, Boost C++, Xaml, Xslt, Xschema, Xpath, Bash, Powershell, VB Practiced: R, Elixir, Erlang, Elm, F#, AngularJS, jQuery, Html5, Css, Svg, Web Audio Api, Underscore.js, Node.js, Octave Small memories of: Prolog, Oz/ML, Pict and Nomadic Pict Wish to learn: Agda, Idris, Coq, Rebol/Red Language Love: English, Deutsch, Español, French (mother tongue), Esperanto (though I no longer practice since at least 10 years, this language is beautiful) Learning: 中文 (Chinese), 日本語 (Japanese) Wish to learn (one day... if possible, or at least some basics elements for calligraphy): Korean, Arabic, Mongolian, Russian, Dutch/Nederland, Italian, Sanskrit, and a bit of Tibetan... let's see.
Updated on June 19, 2022Comments
-
Stephane Rolland 7 months
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 ?
-
Stephane Rolland about 12 yearsOkay, but in both case Leaf and Branch are defined by the language, and I define my own type Tree. isn't it ?
-
sepp2k about 12 years@Stephane: No. In both cases neither the type
Tree
nor the constructorsLeaf
andBranch
existed previously, and you're defining all three of them with yourdata
definition. -
stephen tetley about 12 yearsHi sepp2k - the "general tree" defined in the question is more usually called a rose tree. Sometimes they are implemented with a separate Leaf constructor as above - sometimes as per Data.Tree the Branch constructor carries the data instead.
-
Stephane Rolland about 12 years@Stephen, could you write down the definition of the other implementation of the rose tree you are referring to ?