reckless intuitions of an epistemic hygienist ([info]gustavolacerda) wrote,
@ 2008-05-17 21:37:00
Previous Entry  Add to memories!  Tell a Friend  Next Entry
Lisp<->ML
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.


(7 comments) - (Post a new comment)


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

(Reply to this) (Thread)


[info]gwillen
2008-05-18 05:52 am UTC (link)
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).

(Reply to this) (Parent)(Thread)


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

(Reply to this) (Parent)


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

(Reply to this) (Parent)


[info]wjl
2008-05-18 12:02 pm UTC (link)
oh, I need to be more careful about tuples vs lists.

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

(Reply to this) (Thread)


[info]gustavolacerda
2008-05-19 04:30 pm UTC (link)
How can you cast/convert tuples into lists and vice-versa?

(Reply to this) (Parent)(Thread)


[info]wjl
2008-05-20 06:18 am UTC (link)
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.

(Reply to this) (Parent)


(7 comments) - (Post a new comment)

Create an Account
Forgot your login or password?
Login w/ OpenID
English • Español • Deutsch • Русский…