7 ms·
Can anybody explain the advantages of switching to a functional language when it is not a pure one? For example, how is it better than Javascript? Is the unifor
by amelius 11y ago
Can anybody explain the advantages of switching to a functional language when it is not a pure one? For example, how is it better than Javascript? Is the uniformity of the syntax (and perhaps implicit currying) the main benefit?
- michaelsbradley 11y agoWhich dialect/s of Lisp have implicit currying as a language feature? Or do you have in mind other non-pure functional languages?
- llamaz 11y agoWhen the inputs are similar to the outputs, (e.g. text with linux CLI commands) functions can be very flexible and do things they were not originally designed to do (e.g. linux CLI commands). An assumption that's implicit in what I've said is data flow programming. Data flow programming is when programming is thought of as connecting black boxes with inputs and outputs together. This way of programming is inherently modular. This is to be contrasted with (i) iteration, or recursionn which are inherently un-modular and (ii) The von Neumann model of computation, where there is a separation between code and data, and programming is thought of to be functions acting on the data (the problem with this model is that things need to be executed in a certain order, and sometimes the order isn't explicit in the code, which leads to bugs. Contrast this with data flow programming, where time is modelled explicitly) In LISP, the common interface between functions in the list rather than text. To encourage data flow programming and discourage recusion, map, filter, and fold are provided.
- jwdunne 11y agoI'm not so sure about implicit currying in CL. The uniform syntax that provides homogeny between code and data is exactly the main benefit because, for example, you can go ahead and add implicit currying (and much more). An example result of this benefit is CLOS from memory - a filly featured object system that is composed of extensions to the core language. In fact, I think it has many powerful features not present in archetypical OO languages. Multi-dispatch is one feature I recall from the top of my head.
- JoelMcCracken 11y agoThis really depends upon which functional language you're referring to. Each one has a different set of features. JavaScript is in some sense an impure functional language, however hastily designed in various places. In the context of CL, the canonical example would be "macros". But CL has a bag of other features that makes it appealing.
- kazinator 11y agoLisp isn't a functional language. It's a family of languages, most of which support more than one programming paradigm. Most languages that are identified as some kind of Lisp are strictly evaluated, do not support partial evaluation (explicit currying only), and support mutable variables and mutable aggregate objects.
- technomancy 11y agoMinor nit-pick; the terms here are backwards. Nearly all lisps support explicit partial evaluation, but not many support currying, which is implicit.
- pjmlp 11y agoKnowing Lisp since the days it was the only FP one had access to, I beg to differ. I really disagree with the 21st century fashion to make everything that isn't a Miranda derived language as not being a FP language.
- nikki93 11y agoThis may not be the answer you're looking for, but I think you might get the highest ratio of information-to-time by just trying it out. Second-hand opinions are hard. Practical Common Lisp is the best intro, and two chapters (maybe 40 min) in you'll know if it's worth going further.
- wtbob 11y agoI don't really think of Lisp as a functional language: it's really an imperative language with some functional features, and some OO features, and some aspect-oriented features and so forth. The advantage that I see in switching to Lisp is that it's a nice, pleasant, powerful language in which to work. The standard is well-thought-out. The abstraction capabilities are wonderful. Compared to JavaScript, there's no comparison:-) Just off the top of my head, Lisp has integers, ratios (e.g 1/2), big integers (e.g. (expt 2 2048) → a number so large it breaks the HN page wrap) and complex numbers (e.g. (sqrt -3) → #C(0.0 1.7320508)). Lisp's error-handling system of conditions and restarts is profoundly powerful: one part of code hand signal an error, another can determine how to fix it, and execution can resume without unwinding the stack (as opposed to exceptions, where by the time a higher level of code determines what to do the function encountering the problem has already returned). Technically, one could implement this in JavaScript (it's all based on first-order functions), but since JavaScript doesn't have macros it'd be pretty hideous. Which raises the point of macros, which are enabled by the uniform syntax. They're a powerful tool for syntactic abstraction, just like functions are a powerful tool for procedural abstraction. A language without macros is almost as primitive as one without functions. There are also read-macros, which enable one to customise or even replace the parser (called a reader in lisp). Don't like Lisp? That's okay, you can write a reader which parses JavaScript and returns Lisp objects to be evaluated or compiled! Then there are compile macros, which enable compile-time optimisation. And there are symbol macros, which can make what looks like a variable access actually able to perform any operation. Imagine being able to write 'foo = bar' in JavaScript, and having that assignment be logged. In Lisp, you can do that. One big advantage is that all of these macros systems are … just Lisp. They're not special: you can do anything in a read macro that you can do in your code. You don't have to learn a special syntax or a limited subset of the language: it's all there if you need it. Then there's the object system (which is enabled by macros). CLOS is the most powerful, most convenient object system with which I'm familiar. JavaScript methods belong to a prototype, which means that in foo.bar(baz) only foo determines what bar is; in Lisp, methods are multimethods, and may be specialised on any and all arguments: (bar foo baz) could do different things depending on the type of foo, the type of baz or the types of both. Places are an amazing concept. In most languages there's no way to say 'foo(bar) = baz', but in Lisp one can do (setf (foo bar) baz) with the right setf functions and it Just Works. Lisp has optional type annotations, which can lead to extremely fast code. Then there are little things like well-thought-out equality predicates, cleaner-looking code &c. The advantage of JavaScript over Lisp is that because it is installed everywhere it has a much, much, much larger community, which means more libraries, wider support. But install base and community size are honestly the only real advantages I can think of.
- catnaroek 11y agoA disadvantage of intensional purity (limiting the language to guaranteed-pure constructs, which is enforceable by a compiler) is, ironically, that it gives the programmer less control over how extensionally pure functions (i.e., no observable effects, which is obviously undecidable) are evaluated. In ML (an impure functional language), you can evaluate extensionally pure functions strictly (the straightforward manner), lazily (which Haskell gives you out of the box, but in ML requires reference cells), memoizing previously computed results (which Haskell can't easily do, but in ML can be done with reference cells), etc.
- sklogic 11y agoLisp is not a functional language and should never be used as if it is somehow "functional". Lisp is a homoiconic meta-language, and this is the only thing that matters, not all that functional-schmunctional crap.
- hydandata 11y agoLisp is not a functional programming language. Lisp is a multi-paradigm programming language. Functional programming, although awesome, is not the optimal choice for everything. Designers of Lisp understood this, so the language tries to get out of your way as much as possible. No programming language is perfect sadly, and Lisp too has its warts, but programming in Lisp compared to any other mainstream programming language, once you master it, is like walking up a hill after losing 200 pounds of weight.