4 ms·
>"If all you want to do is e.g. (map somefun mylist), being able to concisely define executable "objects" (closures), inline if necessary, is very useful." It
by pmelendez 11y ago
>"If all you want to do is e.g. (map somefun mylist), being
able to concisely define executable "objects" (closures), inline if necessary, is very useful."
It is interesting because this is exactly how C++11 implements lambdas. Still Scheme's closures are more convenient although C++14 is closing the gap.
- frou_dh 11y agoIn my own scheme-esque language, I have tried hooking up extra-concise means not only for lambdas, but for "anonymous macros" too. (Yes, the readability of the resulting code is very suspect. Just experimenting!) # Provide a prefix syntax that constructs a unary procedure from just a body. (extend-parser "^" (mac (x) `(proc (?) ,x))) # Provide a prefix syntax that constructs a unary macro from just a body. (extend-parser "%" (mac (x) `(mac (?) ,x))) # Apply an anonymous macro to 4, resulting in definitions for cad{1,4}r # being generated then evaluated: ( %`(seq ~(map ^(let i (codepoint-count ?) sym (string-to (sprint "ca" ? "r")) `(def ,sym (proc (xs) (ith-element ,i xs)))) (map ^(apply sprint (repeat "d" ?)) (range 1 ?)))) 4) # What ended up being evaluated at the top-level: # (seq (def cadr (proc (xs) (ith-element 1 xs))) # (def caddr (proc (xs) (ith-element 2 xs))) # (def cadddr (proc (xs) (ith-element 3 xs))) # (def caddddr (proc (xs) (ith-element 4 xs))))
- jjtheblunt 11y agoI think you have a mistake in what you wrote, but please correct me if I am wrong. C++11 lambdas which capture free variables by reference can not be returned from a function in which the free variables occur, and be expected to work, because the function's variable (a free variable to the lambda) may no longer refer to memory allocated for use as the variable. That's not true for Scheme. Scheme's underlying implementation of lambda and function call means that true closures are created: if a lambda refers to a variable which is not local to (or a parameter to) the lambda, then that free variable belongs to an environment, whose lifetime isn't delimited by popping a stack and returns, and is therefore still live as long as some closure (or stack activation frame) refers to it. Return of a lambda with free variables will work in Scheme, and can fail in C++11.
- pmelendez 11y agoYou are correct in the sense that Scheme and C++ handle non-local variables and non-parameters differently (In C++ you can also capture variables by value, but I don't think there is a proper free variable capture mechanism at all). However, that's not what I meant when I wrote that. I was referring this quote: "being able to concisely define executable "objects" (closures), inline if necessary, is very useful" The fact that you can define inline executable objects is indeed very useful, and C++ does exactly that: an inline mangled class instantiated bounded to one variable. It would be cool to have free variable in C++ in the same way that Scheme handles it, but I don't think the language would get there anytime soon (Gee, it is still cumbersome to have recursive lambdas).