Talk:Haskell/More on datatypes
From Wikibooks, the open-content textbooks collection
[edit] treeMap
For learning purposes, why I have removed the comment behind the type signature.
The type of treeMap is (a -> b) -> Tree a -> Tree b. We call treeMap (+1). The types of (+) and 1 are:
(+) :: Num a => a -> a -> a 1 :: Num a => a --Aye, polymorph!
This turns (+1) into a function of type Num a => a -> a
So treeMap is of type Num a => Tree a -> Tree a
This means Haskell doesn't get to be confused between Int and Integer, because both will do.
--Tchakkazulu 16:10, 10 August 2006 (UTC)
[edit] Tree - elements only in leaves
I modified the discussion of Trees in two ways. First, letting the Branch constructor take a value of type a as well as left and right subtrees just complicated the discussion without adding any substance, so I simplified it: the Branch constructor now just takes a left and right subtree. Secondly, treeFold as defined was not a true analog of foldr, as explained in the revised text.
Here is an example that shows one of the problems with the way treeFold was defined. Note that foldr (:) [] is the identity function on lists; with my new definition of treeFold, treeFold Branch id is the identity function on Trees; but with the old definition of treeFold, there is no choice of the arguments f and z that will give you the identity function.
--Kevin S. Van Horn (kevin@ksvanhorn.com), 1 July 2007
Great, I came here to see a discussion about why the definition of Branch differed from the one in YAHT. Are you sure this gives the same structure for the BinaryTree? Seems to me like only Leaves have elements rather than one at every branch.
--thattommyhall