On Wed, Jul 8, 2009 at 1:57 PM, Robert Campbell <rrc...@gmail.com> wrote:

> If it's okay, could somebody explain the difference between what's
> happening here:
>
> user> (def my-func (list + 1 2))
> #'user/my-func
> user> (my-func)
> ; Evaluation aborted.
>
> and here:
>
> user> (def my-func (list + 1 2))
> #'user/my-func
> user> (eval my-func)
> 3
>
> I don't really understand how:
> user>(my-func) is NOT eval on my-func in the REPL. My understanding is
> the first item in the list is treated as a function, with the rest of
> the list passed as arguments. Wouldn't the REPL just be calling eval
> internally on everything you type in?


Not every expression is a function, even if most are function calls.

Any expression can be evaluated with eval. To call something as a function,
though, it has to be something invokable. A list, even a list of a function
and two values, isn't. An actual function is, as produced by defn or fn or
#(). So are sets, maps, and vectors, which will look up the argument key or
index:

user=> (#{'x 'y} 'x)
x
user=> (#{'x 'y} 'z)
nil
user=> ({:key 'val} :key)
val
user=> ({:key 'val} 3)
nil
user=> (['a 'b 'c] 0)
a
user=> (['a 'b 'c] 2)
c
user=> (['a 'b 'c] 3)
#<CompilerException java.lang.ArrayIndexOutOfBoundsException: 3
(NO_SOURCE_FILE:0)>

And keywords can be invoked to do set and map lookups:

user=> (:key {:key 'val})
val
user=> (:x {:key 'val})
nil
user=> (:key #{:key})
:key
user=> (:x #{:key})
nil

Last but not least, there's ., .., .methName, Class., Class/staticMeth,
Class/staticField, and other variations on this theme to interact with Java
classes. And maybe one or two things I'm forgetting. :)

--~--~---------~--~----~------------~-------~--~----~
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