&gt; I&#39;m not happy with any of these options.<br><br>Why are you unhappy with the ImplicitParams option?<br><br>It&#39;s pretty much like resorting to a newtype, as it&#39;s been suggested before.<br><br><div class="gmail_quote">

2012/6/27 Tillmann Rendel <span dir="ltr">&lt;<a href="mailto:rendel@informatik.uni-marburg.de" target="_blank">rendel@informatik.uni-marburg.de</a>&gt;</span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

Hi Rico,<br>
<br>
Rico Moorman wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">


  data Tree = Leaf Integer | Branch (Tree Integer) (Tree Integer)<br>
<br></div><div class="im">
  amount:: Tree -&gt; Integer<br>
  amount (Leaf x) = x<br>
  amount (Branch t1 t2) = amountt1 + amountt2<br>
<br></div>
[...] additional requirement: &quot;If the command-line flag --multiply is set,<div class="im"><br>
the function amount computes the product instead of the sum.&quot;<br>
<br></div><div class="im">
How would you implement this requirement in Haskell without changing the<br>
line &quot;amount (Leaf x) = x&quot;?<br>
</div></blockquote></blockquote>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
The (for me at least) most obvious way to do this would be, to make the<br>
operation to be applied to determine the amount (+ or *) an explicit<br>
parameter in the function&#39;s definition.<div class="im"><br>
<br>
  data Tree a = Leaf a<br>
              | Branch (Tree a) (Tree a)<br></div>
  amount :: (a -&gt; a -&gt; a) -&gt; Tree a -&gt; a<br>
  amount fun (Leaf x) = x<br>
  amount fun (Branch t1 t2) = amount fun t1 `fun` amount fun t2<br>
</blockquote>
<br>
I agree: This is the most obvious way, and also a very good way. I would probably do it like this.<br>
<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Which drawbacks do you see besides increased verbosity?<br>
</blockquote>
<br>
Well, you did change the equation &quot;amount (Leaf x) = x&quot; to &quot;amount fun (Leaf x) = x&quot;. In a larger example, this means that you need to change many lines of many functions, just to get the the value of fun from the point where it is known to the point where you need it.<br>


<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
[...] I am wondering which ways of doing this in Haskell you mean.<br>
</blockquote>
<br>
I thought of the following three options, but see also Nathan Howells email for another alternative (that is related to my option (1) below):<br>
<br>
<br>
(1) Implicit parameters:<br>
<br>
{-# LANGUAGE ImplicitParams #-}<br>
data Tree = Leaf Integer | Branch Tree Tree<br>
<br>
amount :: (?fun :: Integer -&gt; Integer -&gt; Integer) =&gt; Tree -&gt; Integer<div class="im"><br>
amount (Leaf x) = x<br></div>
amount (Branch t1 t2) = ?fun (amount t1) (amount t2)<br>
<br>
<br>
(2) Lexical Scoping:<br>
<br>
data Tree = Leaf Integer | Branch Tree Tree<br>
<br>
amount :: (Integer -&gt; Integer -&gt; Integer) -&gt; Tree -&gt; Integer<br>
amount fun = amount where {<div class="im"><br>
amount (Leaf x) = x<br></div>
; amount (Branch t1 t2) = fun (amount t1) (amount t2) }<br>
<br>
<br>
(3) UnsafePerformIO:<br>
<br>
import System.IO.Unsafe (unsafePerformIO)<br>
<br>
data Tree = Leaf Integer | Branch Tree Tree<div class="im"><br>
<br>
amount :: Tree -&gt; Integer<br>
amount (Leaf x) = x<br></div>
amount (Branch t1 t2) = fun (amount t1) (amount t2)<br>
  where fun = unsafePerformIO ...<br>
<br>
<br>
I&#39;m not happy with any of these options. Personally, I would probably go ahead and transform the whole program just to get the value of fun to where it is needed. Nevertheless, having actually done this before, I understand why Martin Odersky doesn&#39;t like doing it :)<div class="HOEnZb">

<div class="h5"><br>
<br>
  Tillmann<br>
<br>
______________________________<u></u>_________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org" target="_blank">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/<u></u>mailman/listinfo/haskell-cafe</a><br>
</div></div></blockquote></div><br>