Strake | 28 Jun 2012 17:51
Picon

Fwd: Martin Odersky on "What's wrong with Monads"

On 26/06/2012, Nathan Howell <nathan.d.howell <at> gmail.com> wrote:
> On Tue, Jun 26, 2012 at 3:19 PM, Tillmann Rendel
> <rendel <at> informatik.uni-marburg.de> wrote:
>> All fine so far. Now, consider the following additional requirement: "If
>> the
>> command-line flag --multiply is set, the function amount computes the
>> product instead of the sum."
>>
>> How would you implement this requirement in Haskell without changing the
>> line "amount (Leaf x) = x"?
>
> One option is to encode the desired behavior at the type level. By
> extended the data type slightly and adding a Functor instance,
> selecting between a product and a sum can be done using their Monoid
> newtypes: ...

Better yet, use foldMap:

> import Data.Monoid
> import Data.Foldable
> import System.Environment

> data Tree a = Leaf a | Branch (Tree a) (Tree a)

> instance Functor Tree where
>   f `fmap` Leaf x = Leaf (f x)
>   f `fmap` Branch x y = Branch (fmap f x) (fmap f y)

> instance Foldable Tree where
>   foldMap f (Leaf x) = f x
(Continue reading)


Gmane