Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/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.  Applicative on Tree (Mike Houghton)
   2. Re:  Applicative on Tree (Imants Cekusins)
   3. Re:  Applicative on Tree (Imants Cekusins)


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

Message: 1
Date: Mon, 7 Sep 2015 20:59:18 +0100
From: Mike Houghton <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] Applicative on Tree
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8

Hi,

I?m looking at 

data Tree a = Node a [Tree a] deriving (Show)

and trying to write the Applicative instance.
I have I think a correct Functor :


instance Functor (Tree) where
        fmap f (Node x tr) = Node (f x) ( fmap (fmap f) tr)

but I?m well and truly stuck on Applicative :)

I have


instance Applicative (Tree) where
        pure x = Node x []
        (Node f []) <*> tr = fmap f tr
        (Node f fs) <*> tr@(Node x xs) = ?????

I cant get rid of the question marks! :) 
any pointers would be really appreciated!

Thanks





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

Message: 2
Date: Tue, 8 Sep 2015 01:39:04 +0200
From: Imants Cekusins <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Applicative on Tree
Message-ID:
        <cap1qina55rwjye0qm-ksjahe5omrx88dnxf0f_eziz7r3-j...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

> data Tree a = Node a [Tree a]

well here is something that builds and runs. Not sure if the monad laws apply.
watch out for indents!


{-# LANGUAGE InstanceSigs #-}
module TreeApp where

import Debug.Trace
import Data.Char

data Tree a = Node a [Tree a]   deriving (Show)


instance Functor Tree where
   fmap::(a -> b) -> Tree a -> Tree b
   fmap f (Node x l0) = Node (f x) (fmap f <$> l0)


instance Applicative Tree where
   pure::a -> Tree a
   pure a =  Node a []

   (<*>)::Tree (a -> b) -> Tree a -> Tree b
   (<*>) (Node f _) tra = f <$> tra


instance Monad Tree where
   return::a -> Tree a
   return a = pure a

   (>>=)::Tree a -> (a -> Tree b) -> Tree b
   (>>=) (Node x []) amb = amb x
   (>>=) (Node x l0) amb = Node b (m1 <$> l0)
         where (Node b _) = amb x
               m1 ta = ta >>= amb


f::Char->Int
f = digitToInt

mb::Char->Tree Int
mb c = f <$> (pure c)

main::Char -> Char -> IO ()
main a b = print $ tc4 >>= mb
{-
   do
      print ti2
      print ta3
      tm4 <- tc4
-}
   where tc1 = pure a
         ti2 = f <$> tc1
         ta3 = (Node f []) <*> tc1
         tc4 = Node b [tc1]


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

Message: 3
Date: Tue, 8 Sep 2015 02:32:01 +0200
From: Imants Cekusins <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Applicative on Tree
Message-ID:
        <cap1qinbiw1rm_rvsdqizhpisdf-jczpgpdkkgxgadzfkzbp...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8

#2 .. this is probably correct:

{-# LANGUAGE InstanceSigs #-}
module TreeApp where

import Debug.Trace
import Data.Char

data Tree a = Node a [Tree a]   deriving (Show)


instance Functor Tree where
   fmap::(a -> b) -> Tree a -> Tree b
   fmap f (Node x l0) = Node (f x) (fmap f <$> l0)


instance Applicative Tree where
   pure::a -> Tree a
   pure a =  Node a []

   (<*>)::Tree (a -> b) -> Tree a -> Tree b
   (<*>) (Node f []) tra = f <$> tra
   (<*>) tab@(Node f tf0) (Node x l0) = Node (f x) l1
                  where l1 = [fu <*> a | fu <- tf0 , a <- l0]



instance Monad Tree where
   return::a -> Tree a
   return a = pure a

   (>>=)::Tree a -> (a -> Tree b) -> Tree b
   (>>=) (Node x []) amb = amb x
   (>>=) (Node x l0) amb = Node b (m1 <$> l0)
         where (Node b _) = amb x
               m1 ta = ta >>= amb


f1::Char->Int
f1 = digitToInt

mb::Char->Tree Int
mb c = f1 <$> (pure c)

main::Char -> Char -> IO ()
main a b = print $ tc4 >>= mb
   where tc1 = Node a [Node b []]
         ti2 = f1 <$> tc1
         ta3 = (Node f1 []) <*> tc1
         tc4 = Node b [tc1]


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

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


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

End of Beginners Digest, Vol 87, Issue 1
****************************************

Reply via email to