Case Statements

Marko Rauhamaa marko at pacujo.net
Wed Mar 16 13:07:49 EDT 2016


BartC <bc at freeuk.com>:

> On 16/03/2016 14:31, Marko Rauhamaa wrote:
>> Scheme has this:
>
>>     (case (die10)
>>       ((1 3 5 7 9)
>>        => (lambda (n)
>>             n))
>>       (else
>>        => (lambda (n)
>>             (/ n 2))))
>>
>> which maps 1, 3, 5, 7 and 9 onto themselves but halves 2, 4, 6, 8 and
>> 10.
>
> I don't get this; what does the lambda do here? Why not just yield
> either n or n/2?

Scheme is all about lambdas. They are everywhere. The margin of this
posting is too small for me to explain it.

>> As for a chained if/elif, Scheme as "cond:"
>
>>     (cond
>>      ((windy?)
>>       (fly-kite))
>>      ((shining? sun)
>>       (go-out))
>>      ((raining?)
>>       (play-soccer))
>>      (else
>>       (read-book)))
>
> Which is like my Ruby case example. Simple and to the point. (Not sure
> of the significance of ?)

Scheme identifiers can contain punctuation characters. Functions that
return a boolean are conventionally given names that end in a question
mark. Functions that modify their arguments are conventionally given
names that end in an exclamation point.


Marko



More information about the Python-list mailing list