On Tuesday, June 26, 2012 at 6:19 PM, Tillmann Rendel wrote:
Hi,MightyByte wrote:Of course every line of your program that uses a Foo will change if you switchto IO Foo instead.But we often have to also change lines that don't use Foo at all. Forexample, 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 -> Integeramount (Leaf x) = xamount (Branch t1 t2) = amountt1 + amountt2All fine so far. Now, consider the following additional requirement: "Ifthe command-line flag --multiply is set, the function amount computesthe 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 tocall (+) or (*). In particular, we would not touch the other two lines.How would you implement this requirement in Haskell without changing theline "amount (Leaf x) = x"?(I actually see three ways of doing this in Haskell, but all haveserious drawbacks and do not fully solve the problem).Here it seems not so bad just to change all three lines of the amountfunction, even if they are not strictly related to the semantic changewe want to make. But in a real program, this situation can translate tochanging thousands of lines of code in many functions just to implementa minor change to a single requirement.Tillmann_______________________________________________Haskell-Cafe mailing list