[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: call/cc
David Simmons writes:
> So, what is the point of this discussion again?
We were discussing programming in explicit continuation-passing style;
someone pointed out that languages where lambda is verbose and hard to
read make explicit CPS verbose and hard to read too, and so I thought
I'd compare some different languages along the axes of how verbose
their version of 'lambda' was, by creating a lambda and then invoking
it. ()
> I'm not clear on the criteria. For example, why is postscript listed as
> taking only 6 tokens. How is that (actually expressively) different from
> writing the equivalent in a language like Smalltalk:
>
> [3+5] value
It's different in that that's {3 5 add} exec in PostScript, not 3 5
{add} exec. Whether that difference is "actually expressive" is a
matter of opinion. I picked a lambda with two arguments because a
number of languages have special forms either for creating or invoking
a lambda with zero or one arguments.
> Would I write (in postscript):
>
> 3 5 7 {add}
Writing that will push four values on the stack: 3, 5, 7, and a
function. 3 5 7 {add} exec will result in two values on the stack: 3
and 12. If you want to write a function to add three values, you can
write {add add}.
A Scheme fragment mostly isomorphic to 3 5 7 {add} exec is
3 ((lambda (x y) (+ x y)) 5 7)
> You need to separate the anon-fn from the invocation of that anon-fn
> with some set of values.
You're welcome to rewrite all the examples with an intermediate
variable, if you like.
> For classic Smalltalk I think your example form would be:
>
> fn := [:x:y| x+y]
>
> fn value: 3 value: 5.
Yep; I didn't know about #value:value: when I wrote it. (At least in
Squeak, I need a space between :x and :y.)
(#value:value: cuts Smalltalk down from 15 tokens to 14, making it
shorter than Python.)
What's the connection between blocks and continuations?
pixel@mandrakesoft.com writes:
> > 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.
It's not anonymous; it's called +. It's not new; it's a primitive
function.
> in fact, (+) is the same as (\x y -> x + y) via eta-reduction.
Yes, and (+) 3 5 is the same as 8 via constant folding and function
application. But '8' is not a useful answer to the question, "How do
I add two numbers in OCaml?"
> Anyway, you can have the same as your stack-based construct in other
> languages:
>
> Lisp's (funcall '+ 3 5)
That isn't the same; that calls + directly (well, actually, it calls
it after looking up its function binding in the environment). {add}
or :noname + ; or [+] are freshly-created lambda-expressions that
simply call + themselves, but are not themselves identical to +.
> but this is pretty dumb... Maybe a better closure example would be better.
> What about adding the 2 parameters + 1.
These aren't closures, FWIW.
> Perl: sub { $_[0] + $_[1] + 1 }->(3, 5) 20 tokens
> JavaScript: function(x, y){return x + y + 1}(3, 5)
> 19 tokens
Isn't that 18?
Ruby: proc{|x,y|x+y+1}[3,5] 18 tokens (thanks Avi)
> Lisp: ((lambda (x y) (+ x y 1)) 3 5) 17 tokens
> Smalltalk: [:x :y | x + y + 1] valueWithArguments: #(3 5)
> 16 tokens
Isn't that 17?
with #value:value:: [:x :y | x + y + 1] value: 3 value: 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
> FORTH, PostScript, dc???
PostScript: 3 5 {add 1 add} exec 8 tokens
dc: 3 5 [+1+] x 8 tokens
FORTH: 3 5 :noname + 1+ ; execute 7 tokens
I've tried to keep all the languages without n-ary addition operators
implemented as (+ x (+ y 1)) rather than (+ (+ x y) 1).