Discussion:
Good functional programming habbit?
(too old to reply)
Xah Lee
2010-04-29 09:03:51 UTC
Permalink
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.
Many thanks,
Gabriel
using local variable is fine. In 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 depends 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, such as
global variables, 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 program of a given language.

there are, academic language that does not have variables at all.
(though, off hand i don't know any. Web search for the word
“cominator” and you'll find examples.)

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/F#, 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 100 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:

• The Concepts and Confusions of Prefix, Infix, Postfix and Fully
Nested Notations
http://xahlee.org/UnixResource_dir/writ/notations.html

• Fundamental Problems of Lisp
http://xahlee.org/UnixResource_dir/writ/lisp_problems.html

In fact, trying code code in no-variable functional style is part of
the reason 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)

Xah
∑ http://xahlee.org/


Andreas Eder
2010-04-29 21:42:05 UTC
Permalink
Hi Xah,
Xah> using local variable is fine. In lisp, it's like this:

Xah> (let (var1 var2 ...)
Xah> (setq var1 val1)
Xah> (setq var2 val2)
Xah> ...
Xah> body
Xah> )

You can do this easier and more succintly in the following way:

(let ((var1 val1) (var2 val2) ...)
...
body)

'Andreas
--
ceterum censeo redmondinem esse delendam.
Xah Lee
2010-05-02 05:45:56 UTC
Permalink
 XL> if i'd pick on poster's argument that makes the most sense, that'd be
 XL> Kenny's.
Sure, mindless trolls like each other.
Kenny has a blog with photos as well as video and has published
software that won accolades.
I have resume and photos and publications on my site with accolades.

what the fuck is Captain Obvious? did you lead Sinbad and traveled the
7 seas?

i have a poem for u.

in the climb to geekdom,
you have few rungs to catch,
before you see my ass.

Xah
∑ http://xahlee.org/


His kennyness
2010-05-03 00:20:14 UTC
Permalink
Post by Xah Lee
XL> if i'd pick on poster's argument that makes the most sense, that'd be
XL> Kenny's.
Sure, mindless trolls like each other.
Kenny has a blog with photos as well as video and has published
software that won accolades.
And now I am working on Qxells, if only I could get that turd
AllegroServe to find a file. The scary thing is that I can give the
index.html to the browser and it finds the rest of qooxdoo lib stuff
sans problem! Give it to AllegroServe and ... man, not even a 403?!

Last time I did this I ended up moving the entire qooxdoo library into
my application directory!!!!!

This time I have grabbed the AllegroServe source and plan to teach it a
lesson.
Post by Xah Lee
I have resume and photos and publications on my site with accolades.
what the fuck is Captain Obvious? did you lead Sinbad and traveled the
7 seas?
Word up, Xah. But Cap'n O is not a bad guy, just a little wet behind the
ears. Give ''m twenty years, he'll be fine.

kt

Loading...