From: pjb@... (Pascal J. Bourguignon) Date: 2009-01-11T10:19:30+09:00 Subject: Re: functional programming Pit Capitain writes: > 2009/1/10 Pascal J. Bourguignon : >> Brian Candler writes: >>> I guess you rob Peter to pay Paul. In Ruby, a bareword like "biggest" >>> can be a method name (in which case it invokes the method, and evaluates >>> to its return value), or a variable name (in which case it evaluates to >>> the content of that variable). >> >> In Lisp, it can be both, and you always know which you refer. >> >> (defun foo (x) >> (1+ x)) >> >> (let ((foo 41)) >> (foo foo)) >> --> 42 > > Nothing special. In Ruby, that's: > > def foo(x) > x + 1 > end > > foo = 41 > foo(foo) # => 42 > >> And if you happen to store functions in a variable, you have the >> operators FUNCTION and FUNCALL to switch from one namespace to the >> other: >> >> (let ((foo (lambda (x) (1- x)))) >> (list (funcall (function foo) 0) ; calls the function foo >> (foo 0) ; idem >> (funcall foo 0))) ; calls the function bound to the variable foo. >> --> (1 1 -1) > > Can be done as well in Ruby: > > foo = lambda { |x| x - 1 } > [ > send(:foo, 0), > foo(0), > foo.call(0), > ] # => [1, 1, -1] Ok. Last time I tried, I got confusing results. > BTW: Why would I write (1- x) if I want x - 1 ? +1 is a number. 1+ is a symbol (the syntax 1- corresponds to no number). -1 is a number. 1- is a symbol (the syntax 1- corresponds to no number). So we can use these symbols to name functions (defun 1+ (x) (+ x 1)) (defun 1- (x) (- x 1)) so we can (mapcar (function 1+) (list 1 2 3)) or (mapcar (function 1-) (list 1 2 3)) without having to introduce yet another anonymous function. -- __Pascal Bourguignon__