Lisp<->ML

May. 17th, 2008 09:37 pm
gusl: (Default)
[personal profile] gusl
About 8 years ago, I made a multilingual dictionary to help you translate between programming languages (at the time, between C++, VB, JavaScript). I'm now tempted to start the same thing for Lisp<->ML.


Let f be a function of many variables. If the arguments you want to pass are in a list l, how do you apply f to the arguments?

Common Lisp:
(apply f l)

OCaml:
uncurry f l;;


If you have the arguments a, b, how do you apply f?

CL:
(funcall f a b)

Scheme:
(f a b)

OCaml:
f a b;;


oh, I need to be more careful about tuples vs lists.

(no subject)

Date: 2008-05-18 04:40 am (UTC)
From: (Anonymous)
(funcall #'f a b) is equal to (f a b) in CL.

(no subject)

Date: 2008-05-18 05:52 am (UTC)
From: [identity profile] gwillen.livejournal.com
I think Gustavo is having some confusion about the two namespaces in CL? #'f means "f in the function namespace", in which case you could just do (f a b). But if it was (funcall f a b), that's f in the variable namespace, which is different and requires the funcall. Meanwhile in scheme they are both (f a b).

(no subject)

Date: 2008-05-18 06:26 am (UTC)
From: [identity profile] gustavolacerda.livejournal.com
oh right, if the function is bound to a variable, we don't use #' .

(no subject)

Date: 2008-05-18 06:14 am (UTC)
From: [identity profile] gustavolacerda.livejournal.com
If f is an abstract function (e.g. passed as an argument), then you can't simply do (f a b) in CL.

(no subject)

Date: 2008-05-18 12:02 pm (UTC)
From: [identity profile] wjl.livejournal.com
oh, I need to be more careful about tuples vs lists.

yeah, they're not the same in a language with types :P

(no subject)

Date: 2008-05-19 04:30 pm (UTC)
From: [identity profile] gustavolacerda.livejournal.com
How can you cast/convert tuples into lists and vice-versa?

(no subject)

Date: 2008-05-20 06:18 am (UTC)
From: [identity profile] wjl.livejournal.com
well, one answer is "with pattern matching". if you're dealing with 3-tuples, for instance, you could write:
let f (x, y, z) = [x; y; z]
let g [x; y; z] = (x, y, z)

but a better answer is that you don't really want to do this. since there's no way of generically describing the type of an n-tuple, you can only write f and g for each n, and since there's no way of describing the length of a list in its type, g will fail with an exception if applied to any list whose length is not n.

generally, if you have a list of things, you want to do parametric aggregate operations on them, like mapping over them or folding to produce some result. tuples are for passing fixed bits of data into and out of functions. different purposes, so you get different types.

February 2020

S M T W T F S
      1
2345678
9101112131415
16171819202122
23242526272829

Most Popular Tags

Page Summary

Style Credit

Expand Cut Tags

No cut tags