Case Statements

BartC bc at freeuk.com
Wed Mar 16 11:00:32 EDT 2016


On 16/03/2016 14:31, Marko Rauhamaa wrote:
> BartC <bc at freeuk.com>:

>> Even Ruby has one.

>>   case
>>   when this
>>      ....
>>   when that

> That's a different topic.

Yes but, if Ruby has it, why shouldn't Python? (Aren't they rivals or 
something?)

>> which is exactly equivalent to if this... elif that... (when the tests
>> are ordered), with one difference:

> That is no different from a chained if/elif.

That's what I said, but it's an interesting, more symmetric alternative.

> 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?

> 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 ?)

-- 
Bartc




More information about the Python-list mailing list