[Discuss] Functional programming @ UVic (was 'C' string tokenizer for those who hate strtok)

Brian Quinlan brian at sweetapp.com
Mon Jul 3 12:53:03 PDT 2006


Adam Parkin wrote:
>> Yep. Python is an OO language, so it has an OO feel :-) You could use 
>> it functionally, if you really wanted:
> 
> Really?  So functions are first class values and can be passed 
> around/returned from other functions? 

Of course.

> Closures are easy and natural to support? =8-p

% python
 >>> mul = lambda x, y: x * y
 >>> mul(5, 6)
30
 >>> mul(2, 23)
46
 >>> def partial(fn, *args):
...     def inner(*args2):
...             return fn(*(args + args2))
...     return inner
...
 >>> double = partial(mul, 2)
 >>> double(10)
20
 >>> double(50)
100
 >>>

>> a = []
>> a += [5] # instead of a.append(5)
>> a = x[:-1] # instead of pop
>> a = x[1:]  # (cont'd)
> 
> I'm a Perl-person and *I* find that code ugly and cryptic. =;->

I was wrong too. To get the last element in a list:

a[-1]

To get everything BUT the last element in the list:

a[:-1]

Cheers,
Brian


More information about the Discuss mailing list