[Haskell-cafe] Martin Odersky on "What's wrong with Monads"

Dominique Devriese dominique.devriese at cs.kuleuven.be
Thu Jun 28 20:53:09 CEST 2012


2012/6/27 Tillmann Rendel <rendel at informatik.uni-marburg.de>:
> MightyByte wrote:
>>
>> Of course every line of your program that uses a Foo will change if you
>> switch
>> to IO Foo instead.
>
>
> But we often have to also change lines that don't use Foo at all. For
> example, here is the type of binary trees of integers:
>
>  data Tree = Leaf Integer | Branch (Tree Integer) (Tree Integer)
>
> A function to add up all integers in a tree:
>
>  amount:: Tree -> Integer
>  amount (Leaf x) = x
>  amount (Branch t1 t2) = amountt1 + amountt2
>
> 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."
>
> In a language with implicit side effects, it is easy to implement this. We
> just change the third line of the amount function to check whether to call
> (+) or (*). In particular, we would not touch the other two lines.
>
> How would you implement this requirement in Haskell without changing the
> line "amount (Leaf x) = x"?

I may be missing the point here, but having worked on large code bases
with a wide variety contributors before, I find it very advantageous
that programmers are prevented from writing an amount function whose
behaviour depends on command line arguments without at least an
indication in the type. The fact that the function can not perform
stuff like that is precisely the guarantee that the Haskell type gives
me...

Dominique



More information about the Haskell-Cafe mailing list