Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Closure's (apply str) questions?

Tags:

clojure

With closure

(apply str [\a \b])

and

(apply str '(\a \b))

returns "ab".

(apply str (\a \b))

returns an error.

Why is that?

like image 987
prosseek Avatar asked Jul 31 '10 02:07

prosseek


1 Answers

Because (\a \b) means "call the function \a with an argument of \b", and since the character \a is not a function, it fails. Note the difference in the following:

user=> (+ 1 2 3)
6
user=> '(+ 1 2 3)
(+ 1 2 3)

As a general rule, if you want to write a literal sequence, use a vector instead of a quoted list since the quote will also stop evaluation of the parts inside the list, e.g.:

user=> [(+ 1 2) (+ 3 4)]
[3 7]
user=> '((+ 1 2) (+ 3 4))
((+ 1 2) (+ 3 4))
like image 56
Alex Taggart Avatar answered Oct 09 '22 19:10

Alex Taggart