[Haskell-beginners] More simple continuation questions - pipe analogy
martin
martin.drautzburg at web.de
Thu Jul 17 19:13:14 UTC 2014
Am 07/16/2014 05:06 AM, schrieb John Wiegley:
>>>>>> martin <martin.drautzburg at web.de> writes:
>
>> (1) when reading about continuations, there is this thing which needs an
>> "other function" to be passed to produce a
>> final result. And there is this "other function". Which of the two is "the
>> continuation"?
>
> Say you have two functions, foo and bar:
>
> foo :: a -> b
> bar :: b -> c
>
> Ordinarily we'd just compose these, like so:
>
> bar . foo
>
> But there are times when this is not desired or possible. In those cases, we
> can change foo to accept a continuation instead:
>
> foo :: a -> (b -> r) -> r
I see.
I just wondered if unix pipes are a way to get intuition about continuations. Is the following about correct?
flip ($) turns an ordinary value into a Cont. In bash you can achieve the same by prepending "echo" and appending |. So
flip ($) 42
corresponds to
echo 42 |
That expression "echo 42 |" needs another function behind the pipe symbol to return a value. The equivalent to "id" is
"cat". So "flip ($) 42 id" is corresponds to "echo 42 | cat".
I am still struggeling with the naming. If "cat" is the continuation of "echo 42", what is "echo 42" called (the thing
which needs a continuation)?
Anyways
The unix pipe can also be modeled by simple function composition. I can write a longer pipe, e.g.
echo 42 | wc | cat
as
cat . wc $ 42 (assuming there were functions cat and id in haskell)
I have trouble to understand what continuations give me byound that. I suspect the following:
A pipeline is a fixed set of functions, whereas with CPS I can say: if the value received is this, then continue this
way, otherwise continue that way. With a simple pipeline I would have to put all these cases into the function which
makes this decision and even pass the decision on to the next function in the pipeline so it know how we got there. Is
this about right?
Finally, how about the call stack? Are these all tail calls, and I don't have to worry?
More information about the Beginners
mailing list