From: Jim Weirich Date: 2004-02-25T14:51:01+09:00 Subject: Re: Need examples comparing Ruby to Python Tobias Nurmiranta said: > A difference between ruby and python is that functions can easily be > passed as arguments in python. For example a function that returns the > composition of two functions applied to a value. (An artificial example :) > > def compose(f, g, x): > return f(g(x)) > > But then this is even more nicer i scheme. > > (defun (compose f g) > (lambda (x) (f (g x))) ;; returns a function, that can be applied later. > > ((compose 1+ 1+) 1) => 3 The Python and Scheme examples are a bit different. The Scheme code returns a newly created function that is the composition of two functions. The Python code merely returns the result of applying two functions to a value. The Scheme version in Ruby ... oneplus = lambda {|x| x+1 } def compose(f, g) lambda { |x| f[g[x]] } end compose(oneplus, oneplus) [1] #=> 3 The Python version of compose is more like this ... def compose(f, g, x) f[g[x]] end -- -- Jim Weirich jim@weirichhouse.org http://onestepback.org ----------------------------------------------------------------- "Beware of bugs in the above code; I have only proved it correct, not tried it." -- Donald Knuth (in a memo to Peter van Emde Boas)