[Haskell-cafe] do you have to use fix with forkio?

Luke Palmer lrpalmer at gmail.com
Fri Mar 6 16:33:00 EST 2009

```On Fri, Mar 6, 2009 at 1:48 AM, Daryoush Mehrtash <dmehrtash at gmail.com>wrote:

> Question:  Do I need to worry about space leak if I am using the fix to

If you need to worry about a space leak with fix, you need to worry about it
with let.

The reason arrows can tie the loop tighter is more about the nature of
recursion in streams; an arrow "sees" that prior values of a signal are not
used, whereas value recursion is much less restricted.  If, for example, the
arrow were a kleisli arrow over the list monad, this would not be possible.

With the definition fix f = let x = f x in x, you should not see any
performance difference, other than the standard HOF penalty if there is not
enough inlining... but that should not be asymptotic anyway.

Luke

>
> Thanks
>
> Daryoush
> 2009/3/5 Luke Palmer <lrpalmer at gmail.com>
>
>> On Thu, Mar 5, 2009 at 6:27 PM, Donn Cave <donn at avvanta.com> wrote:
>>
>>> Quoth Jonathan Cast <jonathanccast at fastmail.fm>:
>>>
>>> > You can certainly use let:
>>> >
>>> >   reader <- forkIO \$ let loop = do
>>> >       (nr', line) <- readChan chan'
>>> >       when (nr /= nr') \$ hPutStrLn hdl line
>>> >       loop
>>> >     in loop
>>> >
>>> > But the version with fix is clearer (at least to people who have fix in
>>> > their vocabulary) and arguably better style.
>>>
>>> Would you mind presenting the better style argument?  To me, the
>>> above could not be clearer, so it seems like the version with fix
>>> could be only as clear, at best.
>>
>>
>> I like using fix when it's simple rather than let, because it tells me the
>> purpose of the binding.  eg., when I see
>>
>>     let foo = ...
>>
>> Where ... is fairly long, I'm not sure what the purpose of foo is, or what
>> its role is in the final computation.  It may not be used at all, or passed
>> to some modifier function, or I don't know what.  Whereas with:
>>
>>    fix \$ \foo -> ...
>>
>> I know that whatever ... is, it is what is returne, and the purpose of foo
>> is to use that return value in the expression itself.
>>
>> I know that it's a simple matter of scanning to the corresponding "in",
>> but let can be used for a lot of things, where as fix \$ \foo is basically
>> only for simple knot-tying.  Now, that doesn't say anything about the use of
>> fix without an argument (passed to an HOF) or with a tuple as an argument or
>> many other cases, which my brain has not chunked nearly as effectively.  I
>> think fix is best with a single, named argument.
>>
>> Luke
>>
>> _______________________________________________