[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: call/cc



kragen@pobox.com (Kragen Sitaker) writes:

[...]

> No, the FORTH, PostScript, and dc versions of that are 3 5 +, 3 5 add,
> and 3 5+, respectively.  All three of them are, in fact, creating a
> new anonymous function of two arguments which applies the language's
> primitive addition operator to them and returns its result.  It's just
> that in stack-based languages, you don't have to name either your
> formal parameters or your actual parameters.

it is the same for MLs using partial evaluation. 
(+) being is special form of partial evaluation.

but let us re-read what you wanted:

> Here's a little bit of interlingual comparison --- what does it take
> to construct a new anonymous function that adds its two arguments and
> apply it to two numbers?

(+) is an anonymous function which fulfills your definition.

in fact, (+) is the same as (\x y -> x + y) via eta-reduction.

Anyway, you can have the same as your stack-based construct in other
languages:

Lisp's (funcall '+ 3 5)
OCaml ((fun _ -> (+))()) 3 5
Haskell ((\_ -> (+)) undefined) 3 5

but this is pretty dumb... Maybe a better closure example would be better.
What about adding the 2 parameters + 1.

Perl: sub { $_[0] + $_[1] + 1 }->(3, 5)      20 tokens
JavaScript: function(x, y){return x + y + 1}(3, 5)
                                         19 tokens
Lisp: ((lambda (x y) (+ x y 1)) 3 5)     17 tokens
Smalltalk: [:x :y | x + y + 1] valueWithArguments: #(3 5)
                                         16 tokens
Python: (lambda x, y: x + y + 1)(3, 5)   17 tokens
OCaml: (fun x y -> x + y + 1) 3 5            12 tokens
Haskell: (\x y -> x + y + 1) 3 5             12 tokens (\x is 2 tokens, -> is 1)

FORTH, PostScript, dc???

(one may find some shorter lisp expression since the + is n-ary...)