On Jun 14, 10:09 pm, Jared <tri...@gmail.com> wrote:
> Also, I thought this language is functional but I'm able to do change
> declarations in the repl. For example:
> user=> (def x 1)
> #'user/x
> user=> x
> 1
> user=> (def x 2)
> #'user/x
> user=> x
> 2
> user=> (def x (+ 1 x))
> #'user/x
> user=> x
> 3
>
> Why does that happen? This seems to go against the ideas of functional
> programming. I can do the same things with functions too.

It's true that def isn't a strictly functional construct.

That's by design, since def is the construct that allows you to define
and set globally accessible vars, which includes all globally
accessible functions.

If you couldn't re-def a var, you couldn't redefine functions while
coding (or modify/extent existing functions using macros etc). Clojure
(like most Lisps) is a dynamic language with strong support for
interactive development - and you need redefineable vars (or something
similar) for that. The alternative is to recompile/load the whole
program every time you change a function definition. And that sucks
too much.

In idiomatic code, you only use (def) and its variants to set up
globally reachable data/functions across all threads and you don't,
for example, use def as a way to get "variable variables" - you will
get bitten if you try.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to