Talk:Haskell/More on datatypes

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 <tt>Num a => Tree a -> Tree a</tt>

This means Haskell doesn't get to be confused between <tt>Int</tt> and <tt>Integer</tt>, because both will do.

--Tchakkazulu 16:10, 10 August 2006 (UTC)

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 <tt>foldr []</tt> is the identity function on lists; with my new definition of treeFold, <tt>treeFold Branch id</tt> is the identity function on Trees; but with the old definition of treeFold, there is no choice of the arguments <tt>f</tt> and <tt>z</tt> 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

Problematic example in General Map
As of now, the general map example for the Weird type eventually reaches this point:

weirdMap :: (a -> c) -> (b -> d) -> Weird a b -> Weird c d weirdMap fa fb = weirdMap' where weirdMap' (First x)         = First (fa x)     weirdMap' (Second y)         = Second (fb y)     weirdMap' (Third ((x,y):zs)) = Third ( (fa x, fb y) : weirdMap' (Third zs)) weirdMap' (Fourth w)        = --More to follow

However, this expression

weirdMap' (Third ((x,y):zs)) = Third ( (fa x, fb y) : weirdMap' (Third zs))

is wrong since it tries to cons an (a,b) pair to something that is not a list (weirdMap' (Third zs) evaluates to a Weird c d). For that to work you'd need to extract the zs and provide a base case for the recursion:

weirdMap' (Third []) = Third [] weirdMap' (Third ((x,y):zs)) = Third ( (fa x, fb y) : ( (\(Third z) -> z) (weirdMap' (Third zs)) )

Needless to say, that is horribly awkward, and is probably best left to an exercise (say, "rewrite weirdMap' (Third z) making the recursion applied to the list z explicit). Also, the example is followed by:


 * First we change (a,b) into (fa a, fb b). Next we need the mapped version of the rest of the list to add to it. Since we don't know a function for a list of (a,b), we must change it back to a Weird value, by adding Third. This isn't really stylish, though, as we first "unwrap" the Weird package, and then pack it back in. This can be changed into a more elegant solution, in which we don't even have to break list elements into tuples!

I also take issue with the "we don't even have to break list elements into tuples" part, which doesn't really make sense (if the author was referring to breaking down the list into its elements, map is doing that implicitly). I will probably rewrite this part in the next few hours. --Duplode (talk) 01:41, 30 April 2010 (UTC)

Left vs. Right
I was not aware that this had been edited multiple times before (revision history), but Sgronblo is right— should be used for representing errors, per the official documentation. Nyuszika7H (discuss • contribs) 13:59, 26 November 2014 (UTC)