Documentation, assignment in expression.

Kiuhnm kiuhnm03.4t.yahoo.it
Mon Mar 26 07:58:25 EDT 2012


On 3/26/2012 13:13, Jussi Piitulainen wrote:
> Kiuhnm writes:
>> On 3/26/2012 10:52, Devin Jeanpierre wrote:
>>> On Sun, Mar 25, 2012 at 11:16 AM, Kiuhnm
>>> <kiuhnm03.4t.yahoo.it at mail.python.org> wrote:
>>>> On 3/25/2012 15:48, Tim Chase wrote:
>>>>>>>>>> The old curmudgeon in me likes the Pascal method of using "=" for
>>>>> equality-testing, and ":=" for assignment which feels a little closer to
>>>>> mathematical use of "=".
>>>>>>>>>>>> Unfortunately, ":=" means "is defined as" in mathematics. The "right"
>>>> operator would have been "<-".
>>>>>>>>> "Is defined as" is actually pretty reasonable. "Define this to be
>>> that" is a common way to speak about assignment. Its only difference
>>> is the present tense. For example, in Python, "def" stands for
>>> "define", but we can overwrite previous definitions::
>>>>>> def f(x): return x
>>> def f(x): return 2
>>> f(3) == 2
>>>>>> In fact, in pretty every programming language that I know of with a
>>> "define" assignment verb, this is so. For example, in Scheme, x is 2
>>> at the end::
>>>>>> (define x 1)
>>> (define x 2)
>>> x
>>>> When you write
>> (define x 1)
>> (define x 2)
>> x
>> or, in F# and OCaml,
>> let x = 1
>> let x = 2
>> x
>> you're saying
>> x = 1
>> {
>> x = 2
>> x
>> }
>> You don't modify 'x': you hide it by defining another "value" (not
>> variable) with the same name.
>> Indeed,
>> let x = 1
>> let x = 2
>> x
>> is shorthand for
>> let x = 1 in
>> let x = 2 in
>> x
>> No, Devin is right about Scheme. On "top level" re-definition is
> interpreted as assignment. The following mean the same:
>> (define x 1) (define x 2) x
> (define x 1) (set! x 2) x
>> Local definitions in the beginning of a "body" do not allow duplicate
> names at all. The following mean the same:
>> (let () (define x 1) (define y 2) x)
> (letrec* ((x 1) (y 2)) x) ;letrec in older versions (not sure of R6RS)
>> But (let () (define x 1) (define x 2) x) is still an error. Some
> implementations may give it a meaning. Not sure.

Thanks for the correction. I haven't written a line of code in Scheme 
for 15 years and it shows :(
Kiuhnm


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /