Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  Functor on Tree data constructor (Nishant)
   2. Re:  Functor on Tree data constructor (Frerich Raabe)
   3. Re:  Functor on Tree data constructor (Daniel Trstenjak)
   4. Re:  Functor on Tree data constructor (Gesh)
   5. Re:  Functor on Tree data constructor (Tony Morris)


----------------------------------------------------------------------

Message: 1
Date: Wed, 2 Apr 2014 16:23:33 +0530
From: Nishant <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] Functor on Tree data constructor
Message-ID:
        <CAEQDmz7v_oi=6stkyxznieibpsipoir_b6tbsmk05v9of5g...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"

Hi,

Can some explain me the error and a possibly  a fix :

data Tree a = NULL | Node (Tree a) a (Tree a) deriving Show

instance Functor Tree where
............fmap f NULL = NULL
............fmap f (Node left x right) | (left  == NULL) && (right == NULL)
= Node left (f x) right
................................................... | (left  /= NULL) =
Node (fmap f left) x right
................................................... | (right /= NULL) =
Node left x (fmap f right)

ghci gives:

Prelude> :load programs\tree.hs
[1 of 1] Compiling Main             ( programs\tree.hs, interpreted )

programs\tree.hs:32:78:
Couldn't match type `a' with `b'
      `a' is a rigid type variable bound by
          the type signature for fmap :: (a -> b) -> Tree a -> Tree b
          at programs\tree.hs:31:7
      `b' is a rigid type variable bound by
          the type signature for fmap :: (a -> b) -> Tree a -> Tree b
          at programs\tree.hs:31:7
    Expected type: Tree b
      Actual type: Tree a
    In the first argument of `Node', namely `left'
    In the expression: Node left (f x) right
    In an equation for `fmap':
        fmap f (Node left x right)
          | (left == NULL) && (right == NULL) = Node left (f x) right
          | (left /= NULL) = Node (fmap f left) x right
          | (right /= NULL) = Node left x (fmap f right)
Failed, modules loaded: none.

Regards.
Nishant
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20140402/97bd8b3b/attachment-0001.html>

------------------------------

Message: 2
Date: Wed, 02 Apr 2014 13:07:34 +0200
From: Frerich Raabe <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Functor on Tree data constructor
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed

On 2014-04-02 12:53, Nishant wrote:
> Can some explain me the error and a possibly ?a fix :?
>
> data Tree a = NULL | Node (Tree a ) a (Tree a)
>
> instance Functor Tree where
>     fmap f NULL = NULL
>     fmap f (Node left x right) | (left  == NULL) && (right == NULL) = 
> Node left (f x) right
>                                | (left  /= NULL) = Node (fmap f left) 
> x right
>                                | (right /= NULL) = Node left x (fmap 
> f right)

[..]


> programstree.hs:32:78:
> Couldn't match type `a' with `b'
> ? ? ? `a' is a rigid type variable bound by
> ? ? ? ? ? the type signature for fmap :: (a -> b) -> Tree a -> Tree b
> ? ? ? ? ? at programstree.hs:31:7
> ? ? ? `b' is a rigid type variable bound by
> ? ? ? ? ? the type signature for fmap :: (a -> b) -> Tree a -> Tree b
> ? ? ? ? ? at programstree.hs:31:7
> ? ? Expected type: Tree b
> ? ? ? Actual type: Tree a
> ? ? In the first argument of `Node', namely `left'

The issue is that in 'fmap f (Node left x right)', 'left' has type 
'Tree a'. Your 'fmap' function should yield a 'Tree b'though, i.e. the 
'Node' constructor should take a 'Tree b' as well - and 'left' doesn't 
fit.

You can fix this by using 'NULL' directly, e.g.

instance Functor Tree where
     fmap f NULL = NULL
     fmap f (Node left x right) | (left  == NULL) && (right == NULL) = 
Node NULL (f x) NULL
                                | (left  /= NULL) = Node (fmap f left) 
(f x) NULL
                                | (right /= NULL) = Node NULL (f x) 
(fmap f right)

...note that instead of 'x' you also have to use 'f x' (which is also 
the whole point of a Functor instance).

-- 
Frerich Raabe - [email protected]
www.froglogic.com - Multi-Platform GUI Testing


------------------------------

Message: 3
Date: Wed, 2 Apr 2014 13:12:09 +0200
From: Daniel Trstenjak <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Functor on Tree data constructor
Message-ID: <20140402111209.GA8573@machine>
Content-Type: text/plain; charset=iso-8859-1


Hi Nishant,

> fmap f (Node left x right) | (left ?== NULL) && (right == NULL) = > Node left 
> (f x) right

The function 'f' maps from 'a -> b' and 'left' and 'right' have the type 'Tree 
a',
by not mapping 'left' and 'right' with 'f', you're telling the compiler
that the types 'a' and 'b' are equal, but the compiler doesn't consider
these types to be equal.

You also have to map 'left' and 'right':

instance Functor Tree where
   fmap f NULL                = NULL
   fmap f (Node left a right) = Node (fmap f left) (f a) (fmap f right)


Greetings,
Daniel


------------------------------

Message: 4
Date: Wed, 02 Apr 2014 14:37:03 +0300
From: Gesh <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Functor on Tree data constructor
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

>On 2014-04-02 12:53, Nishant wrote:
>> Can some explain me the error and a possibly ?a fix :?
>>
>> data Tree a = NULL | Node (Tree a ) a (Tree a)
>>
>> instance Functor Tree where
>>     fmap f NULL = NULL
>>     fmap f (Node left x right) | (left  == NULL) && (right == NULL) =
>
>> Node left (f x) right
>>                                | (left  /= NULL) = Node (fmap f left)
>
>> x right
>>                                | (right /= NULL) = Node left x (fmap 
>> f right)
Note that you can simplify your instance declaration to:
> instance Functor Tree where
>     fmap f NULL = NULL
>     fmap f (Node left x right) = Node (f' left) (f x) (f' right)
>       where f' x = if x == NULL then NULL else fmap f x
Also note that the NULL in the then clause differs from x. Let f :: a -> b, 
then x :: Tree a and the NULL in that clause :: Tree b. These values are as 
distinct as 2 :: Int and 2.0 :: Double.
HTH,
Gesh


------------------------------

Message: 5
Date: Thu, 03 Apr 2014 00:42:39 +1300
From: Tony Morris <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] Functor on Tree data constructor
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8

On 03/04/14 00:37, Gesh wrote:
>> On 2014-04-02 12:53, Nishant wrote:
>>> Can some explain me the error and a possibly  a fix : 
>>>
>>> data Tree a = NULL | Node (Tree a ) a (Tree a)
>>>
>>> instance Functor Tree where
>>>     fmap f NULL = NULL
>>>     fmap f (Node left x right) | (left  == NULL) && (right == NULL) =
>>> Node left (f x) right
>>>                                | (left  /= NULL) = Node (fmap f left)
>>> x right
>>>                                | (right /= NULL) = Node left x (fmap 
>>> f right)
> Note that you can simplify your instance declaration to:
>> instance Functor Tree where
>>     fmap f NULL = NULL
>>     fmap f (Node left x right) = Node (f' left) (f x) (f' right)
>>       where f' x = if x == NULL then NULL else fmap f x
> Also note that the NULL in the then clause differs from x. Let f :: a -> b, 
> then x :: Tree a and the NULL in that clause :: Tree b. These values are as 
> distinct as 2 :: Int and 2.0 :: Double.
> HTH,
> Gesh
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners

instance Functor Tree where
    fmap f NULL = NULL
    fmap f (Node left x right) = Node (f' left) (f x) (f' right)
      where f' = fmap f



-- 
Tony Morris
http://tmorris.net/



------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 70, Issue 2
****************************************

Reply via email to