4 ms·
Languages which include functional features such as lambda, map, and tail call optimization are highlighted in green. I found one minor inconsistency: Lua has
by bayareaguy 17y ago
Languages which include functional features such as lambda, map, and tail call optimization are highlighted in green.
I found one minor inconsistency: Lua has all these but is still colored blueish.
- chops 17y agoIt doesn't have a native map command, but that's a rather trivial implemention. But I agree, it should be colored green in the "functional programming" section.
- silentbicycle 17y agoLua isn't usually associated with the functional languages. Besides mainly being promoted for embedded / extension use, I think the single biggest reason for this is that, while it has tail-call elimination, closures, first class functions, etc., you still have to say "return x" at the end of functions. I do a lot of FP-style stuff in Lua, and while the language doesn't actively resist it the way Python does, it's not the main direction its design flows. It seems like a small thing syntactically, but languages that return the result of their last expression by default (I think of them as "expression" languages), and that require an explicit block for side-effects (begin in Scheme and OCaml, progn in Common Lisp, etc.), tend to get idiomatically used in a functional manner, while languages that default to a series of statements with side-effects and require an explicit return at the end ("statement" languages) typically do not. While you can use several of the latter languages for functional programming, only the former are functional "by default". Saying "function(x) return x + 1 end" is a tiny bit more trouble than "fun x -> x + 1" or "(lambda (x) (+ x 1))", so while lambdas still get used where they're the best fit (as arguments for map or filter, for example), they're less common overall. Defaults influence a language's overall style at least as much as what's theoretically possible. Python's concept of what's "pythonic" seems to be the most explicit acknowledgement of this. Incidentally, I would love to see a language that crossed Lua with the ML family: a small, portable, clean implementation that interfaces easily with C, but with type inference, ML's top-notch module system, and (ideally) Haskell-style type-classes. OCaml is such a good language that I'm willing to tolerate its warts, but I'd strongly prefer a minimal ML-like language. (FWIW, I haven't used SML, though that's just because I do most of my programming on OpenBSD/amd64 and the smlnj port is i386 only.) When standalone, Lua is best suited to small/medium projects. Its historical emphasis on embedding in an existing project has made a module system for standalone use a bit of an afterthought.* As a project gets larger, static checking and stabilizing the module interfaces becomes far more important, and this is one area the ML languages really excel. * For a good summary, see "Almost Good Enough to Scale: A Lua Mail Handler and Spam Filter" (http://www.lua.org/wshop08.html#ramsey http://www.lua.org/wshop08.html#ramsey)
- gruseom 17y agoyou still have to say "return (val)" at the end of functions We have a bunch of code that compiles to both Common Lisp and Javascript, and those nasty "return" statements are one of the two biggest mismatches between the two languages (the other being Javascript's weird semantics around null, false, 0, and ""). One of these days I intend to try inserting them all automatically.
- bayareaguy 17y agoYes. Playing around with Metalua[1]'s anonymous functions[2] which allow you to shorten function(arg1, arg2, argn) return some_expr end to the much more concise |arg1,arg2,argn| some_exp makes me wish they were part of core Lua. 1- http://lua-users.org/wiki/MetaLua http://lua-users.org/wiki/MetaLua 2- http://metalua.luaforge.net/manual003.html#toc3 http://metalua.luaforge.net/manual003.html#toc3
- silentbicycle 17y agoIndeed. It's surprising how deeply eliminating return changes the language semantics. I've postponed learning Javascript until recently, because my work hasn't usually been web-oriented, but it's interesting how similar Javascript and Lua are. They seem to have been aimed at the same target, but for historical reasons Javascript's development got frozen early, while Lua had time to iron out many similar design flaws. (In Lua, undefined and null are both nil, nil and false are "false-y", and everything else, including 0 and "", are truthy.)
- gruseom 17y agoI think you hit the nail on the head earlier. It isn't "return" as such, it's expressions vs. statements. Backus got it right. What's surprising is that the superior way gets the tiny minority of usage. (Not so surprising if you know the historical reasons.) JS is not so bad. We're lucky that what we're doing (writing high-level FP code that compiles to JS and runs in pretty much every web browser in the world) is doable at all. JS even has its own version of progn: "(a,b,c)" evaluates to c (after evaluating a and b). We use that pretty heavily. Alas, it has two problems: there are some things you can't do inside the block (like declare new variables or have for loops), and it makes the JS harder to read and debug. Otherwise we'd probably just compile everything that way.