Send Beginners mailing list submissions to
        beginners@haskell.org

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
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

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


Today's Topics:

   1.  Hutton 2016 ex8.3a (trent shipley)
   2. Re:  Hutton 2016 ex8.3a (Rishi Rajasekaran)


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

Message: 1
Date: Mon, 3 Sep 2018 02:05:32 -0700
From: trent shipley <trent.ship...@gmail.com>
To: Haskell Beginners <beginners@haskell.org>
Subject: [Haskell-beginners] Hutton 2016 ex8.3a
Message-ID:
        <caeflybk8+h2np6hkjtlcry5juqlh9mdypq_f6v1u5p_+avm...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Given
data Tree a = Leaf a | Node (Tree a) (Tree a)

Write a leaf counter.

Hutton suggests:

leaves :: Tree a -> Int
leaves (Leaf _) = 1
leaves (Node l r) = leaves l + leaves r

I tried:

leavesTrent :: Tree a -> Int
leavesTrent = leaves' 0
         where
           leaves' n (Leaf a) = n + 1
           leaves' n (Node l r) = (leaves' n l), (leaves' n r)

The idea is:

If it is a leaf, add one to the accumulator.  (Following Hutton's
explanation of how sum works if defined with foldl.)   If it is a tree,
proceed down the left subtree recursively, until you get to a leaf, then
roll up to the right subtree.  The problem (among the problems) is that I
don't know how to tell the compiler to do all lefts, before backing up to
go right.  I only know how to do that using a real operator like "+" or foo
(l, r).

Is that kind of no-op recursive branching possible?

Trent.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20180903/acad9f16/attachment-0001.html>

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

Message: 2
Date: Mon, 3 Sep 2018 15:35:44 +0530
From: Rishi Rajasekaran <rajasekaran.ri...@gmail.com>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: Re: [Haskell-beginners] Hutton 2016 ex8.3a
Message-ID:
        <CAKBK7aPCaCXR10k=7O=gh0bvz4fywh+dvw+edae8toheox3...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Hi Trent

For executing your approach, you can do the following:
1. Compute the number of leaves in the left subtree first.
2. Pass that computed value into leaves' call for the right subtree

Regards

Rishi


On Mon, 3 Sep, 2018, 2:36 PM trent shipley, <trent.ship...@gmail.com> wrote:

> Given
> data Tree a = Leaf a | Node (Tree a) (Tree a)
>
> Write a leaf counter.
>
> Hutton suggests:
>
> leaves :: Tree a -> Int
> leaves (Leaf _) = 1
> leaves (Node l r) = leaves l + leaves r
>
> I tried:
>
> leavesTrent :: Tree a -> Int
> leavesTrent = leaves' 0
>          where
>            leaves' n (Leaf a) = n + 1
>            leaves' n (Node l r) = (leaves' n l), (leaves' n r)
>
> The idea is:
>
> If it is a leaf, add one to the accumulator.  (Following Hutton's
> explanation of how sum works if defined with foldl.)   If it is a tree,
> proceed down the left subtree recursively, until you get to a leaf, then
> roll up to the right subtree.  The problem (among the problems) is that I
> don't know how to tell the compiler to do all lefts, before backing up to
> go right.  I only know how to do that using a real operator like "+" or foo
> (l, r).
>
> Is that kind of no-op recursive branching possible?
>
> Trent.
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://mail.haskell.org/pipermail/beginners/attachments/20180903/0e74b1a7/attachment-0001.html>

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

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


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

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

Reply via email to