Assignment versus binding

Ben Bacarisse ben.usenet at bsb.me.uk
Tue Oct 4 15:20:26 EDT 2016


Steve D'Aprano <steve+python at pearwood.info> writes:

> On Tue, 4 Oct 2016 11:27 pm, Ben Bacarisse wrote:
>
>> Haskell defines let (it's version of multiple mutually recursive
>> bindings) in terms of the least fix point of a lambda function whose
>> (pattern) parameter binds the expressions in the definitions.
>
> It binds *the expression* itself? Not the value of the expression?

Yes and no.  The result is as if the value is bound, but (a) Haskell is
lazy so in some sense it is the expression that is bound and (b) this is
a definition of the semantics, not Haskell itself.  The least fixed
point operator has the effect of binding all the names in the expression
to the result of binding all the names in expression to the result of
binding all the names in the expression to the result of...

In effect I'm not entirely sure how to answer your question (but I
repeat that this is how a syntactic feature is defined, not how you
write Haskell).

> So Haskell uses some form of pass by name?

No, just lazy evaluation.

> And (shamelessly using Python syntax) if I have a function:
>
>
> def spam(x):
>     print(x)
>     print(x+1)
>
>
> and then call it:
>
> spam(time.sleep(60) or 1)
>
>
> it will sleep for 60 seconds, print 1, then sleep for another 60 seconds,
> then print 2. Is that right?

Because Haskell is a Lazy language, the argument is not evaluated at the
point of call -- it's only evaluated when x is needed.  That part is
like call by name and "binding the expression and not the value".  But
because it is also a purely function language, the result of an
evaluation must always be the same, so the expression, once evaluated is
not evaluated again, and in that sense it's not like call by name.

The question came up because (I paraphrase) "even Scheme uses
assignment to explain mutually recursive definitions".  Haskell defines
them using the fixed point operator.  It's not really about Haskell
programs so much as how the language features are defined.

-- 
Ben.



More information about the Python-list mailing list