[prev in list] [next in list] [prev in thread] [next in thread]
List: ruby-talk
Subject: Re: Need examples comparing Ruby to Python
From: "Jim Weirich" <jim () weirichhouse ! org>
Date: 2004-02-25 5:51:01
Message-ID: 34020.216.23.54.200.1077688256.squirrel () weirichhouse ! org
[Download RAW message or body]
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)
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic