I've been thinking and talking a lot about programming language design recently. I find when discussing the relative merits of languages, I often get hung up on the question of what is and isn't possible in a decent way in any given language. So I've decided to compile a set of the ways to solve a particular problem in several different languages. Hopefully this is the first of a series.
Problem: define a function "adder" of one variable (x) which returns another function of one variable (y) that sums x + y
My answers for every programming language I've thought about or used recently, in order of decreasing length:
Javascript:
function adder(x){ return function(y){ return x + y; } }
Scheme:
(define adder (lambda (x) (lambda (y) (+ x y))))
Ruby (a Proc is not exactly like a function because Ruby is dirty):
def adder(x) Proc.new {|y| x + y }; end
Erlang:
adder(X) -> fun(Y) -> X + Y end.
Forth:
: adder quote [+ ] append ;
Arc:
(def adder (x) [+ _ x ])
Haskell (currying is cheating):
adder = (+)
C and Java got skipped because the question isn't really meaningful for them, since they don't have anonymous functions.