Avoiding Variables in Functional Programing
On 2010-04-28, Zadirion wrote in comp.lang.lisp:
I'm new to functional programming, just barely getting the hang of it, I hope. So from what I understand, side-effects are not desirable when writing functional code. This means setq and variables in general should be avoided.
But suppose I have a list and I need its length not once, but twice or more inside my function. If i'm not supposed to use a variable to store the length of the list, how am I supposed to reuse the result of that computation (the length determining)?(setq len (length mylist)) (concatenate 'list (subseq 0 (/ len 2)) (subseq (+ (/ len 2) 1) len))
How am i supposed to achieve this without using the len variable? Or am I incorrectly understanding what functional programming is all about? (I come from a c++ background so imperative programming is all I know for now) I could call length each time where needed, but that is obviously very inefficient and unnecessary.
using local variable is fine. In (emacs) lisp, it's like this:
(let (var1 var2 …) (setq var1 val1) (setq var2 val2) … body )
the key to remember about functional programing, is that the behavior of your function should only depend on input, and that usually means just the argument it receives. Using local variable is still functional programing, as long as those variables don't leak (For example, global var is bad), and they shouldn't keep a state (such as in OOP).
However, the quest to avoid using variables is of theoretical interest as well as a interesting programing problem of a given language.
There are, academic languages that do not have variables at all. (For example, Unlambda. A little web search for the word “combinator language” and i also found Joy (programming language) in few seconds, am sure there are others.)
Without a pure functional lang such as Haskell, avoiding variable is difficult or impossible.
Even Haskell, for practical reasons, have local variables, just that you can't change a variable's value. (effectively, they are local “constants”)
In a less strict functional lang OCaml, you can have variables that changes value.
Lisp, as a functional lang, is even less strict than the above ones. Also, it is IMPOSSIBLE to write any non-trivial function without using variables, mutable or not.
In Mathematica, which i've coded for over 10 years, am among the world's say top 1k expert, i often avoid using ANY variables at all. It is not difficult to do. Basically, any function is all just a sequence of application of functions. Though, the code get complicated for average programers to understand, but simple if you understand the function application paradigm.
Mathematica can be considered as a lisp variant. However, lisps (Common Lisp, Scheme Lisp, Emacs Lisp, Arc lisp, newLISP, all can not avoid variables. I'm pretty sure this includes Clojure lisp, Qi lisp, or, that these two still would cost far more effort or un-natural code to avoid vars than Mathematica)
Part of the reason why it is impossible to avoid using variables in lisp, i'v detailed in the past, in these essays:
In fact, trying to code in no-variable functional style contributed to how i discovered these lisp problems.
Look for the sections that discuss the nesting problem. The nesting syntax is part of the problem, but there are other reasons that i haven't discussed in the above essays. Maybe i'll write a detailed essay about this topic in the future, but now just from the top of my head … (was going to write perhaps a one sentence description of what i thought are the other reasons other than the nesting syntax, because the problem cannot theoretically be just syntax… but am tired, and i think the issue is more complex then i can think now… will need to think about this another time)
Originally posted to comp.lang.lisp at Source.