Andy Smith writes:

> I came across the following issue when doing problem 
>
> user=> (let [f '(+ 1 1)] (apply (first f) (rest f)))
> 1
>
> I could use eval but eval is bad (apparently)...
>
> Investigating this further I can see that :
>
> user=> (let [f '(+ 1 1)] (type (first f)))
> clojure.lang.Symbol
>
> wheras :
>
> user=> (type +)
> clojure.core$_PLUS_
>
> It would seem that I still need to call eval on the '+ at least, in order 
> to resolve it to a function, but eval is still bad (apparently).
>
> I could create a map of symbol to function and I can see that others have 
> done the same, but this seems a bit verbose because clojure will do that 
> for me with eval. If the map is the correct approach then why is it any 
> better than eval? 
>
> What is the correct thing to do here? Im sure this is revealing some 
> fundamental misunderstanding that I have... :o/

You want to read about the connection between Vars, Namespaces and
Symbols. Please don't see the following as a good summary for these
topics, it just illustrates the concepts in the scope of your problem:

A var, for example #'clojure.core/+, is a way to refer to a storage
location. #'clojure.core/+ is a var which 'contains' a function which
adds numbers.

A Namespace (for example clojure.core) is just a collection of vars. 

A symbol is a form of identifier. '+ is a symbol.

You start with the symbol +. The function `(ns-resolve ns sym)' will
try to find a var in `ns' with the same name as `sym' and return it.

In the end, you want to call the function behind the var, so you have to
use `var-get' to get the value the var points to.

-- 
Moritz Ulrich

Attachment: pgphkZl7qIu1U.pgp
Description: PGP signature

Reply via email to