[Tutor] Text processing and functional programming?
Karl Pflästerer
sigurd at 12move.de
Thu Aug 14 17:49:45 EDT 2003
On 14 Aug 2003, Danny Yoo <- dyoo at hkn.eecs.berkeley.edu wrote:
[great stuff]
> I still think, though, that it's worthwhile to know how to use functional
> programming techniques, sooner or later, because there are some very cool
> things that are possible with them. Functional programmers often focus on
> writing small, well-defined functions, and more importantly, know how to
> make functions that can create other functions. Here's a small example:
> ###
>>>> def make_mapper(function):
> ... def new_function(L):
> ... return [function(x) for x in L]
> ... return new_function
> ...
And further on think about closures. They allow nice constructs (people
who know Common Lisp or Scheme use them all the time).
In [2]: def make_counter(start=0, step=1):
....: def cfun(step=step, counter=[start]):
....: counter[0] += step
....: return counter[0]
....: return cfun
...:
In [3]: c = make_counter()
In [4]: c()
Out[4]: 1
In [5]: c()
Out[5]: 2
In [6]: d = make_counter(10,2)
In [7]: d()
Out[7]: 12
In [8]: d()
Out[8]: 14
Karl
--
Please do *not* send copies of replies to me.
I read the list
More information about the Tutor
mailing list