Thursday, April 23, 2009

Solutions to Chapter 3 (p. 60)

#1. Given the List definition:

data List a = Cons a (List a)
            | Nil
              deriving (Show)

We can define toList thus:

toList (Cons a as) = a : toList as
toList Nil = []

Each line replaces the List constructor with the matching [a] constructor: Cost is replaced by :, whereas Nil is replaced by an empty list. Because the built-in list type [a] is defined using recursion, so is this conversion process.

#2.

data Tree a = Node a (Maybe (Tree a)) (Maybe (Tree a))
              deriving (Show)

3 comments:

  1. to #2:
    Nice, but the empty tree is not possible.

    .. a newbie
    ReplyDelete
  2. Indeed! Then how about:

    data Tree a = Maybe (Node a (Tree a) (Tree a)) deriving (Show)

    The left and right children always exist, but each of them is a "Maybe" since the tree itself is a Maybe.
    ReplyDelete
  3. data Tree a = Maybe (Node a (Tree a) (Tree a)) deriving (Show)

    Not in scope: type constructor or class `Node'
    ReplyDelete