Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure: Difference between a list and a function that returns a list

Tags:

clojure

I'm a Clojure newbie. I'm trying to understand why the second form doesn't work:

First form:

user=>(def nums(range 3))
(0 1 2)
user=>(map #(list %1) nums)
((0) (1) (2))

Second form:

user=> (map #(list %1) (0 1 2))
java.lang.ClassCastException: java.lang.Integer cannot be cast to clojure.lang.IFn 
(NO_SOURCE_FILE:0)
like image 919
Eduardo Yáñez Parareda Avatar asked Nov 30 '22 15:11

Eduardo Yáñez Parareda


2 Answers

The problem is the expression (0 1 2), which is interpreted as 0 applied to 1 and 2; that's impossible because 0 isn't a function.

(map #(list %1) '(0 1 2))

works as intended, though.

like image 166
Fred Foo Avatar answered Dec 05 '22 09:12

Fred Foo


Because (0 1 2) means call function 0 with args 1 and 2, but 0 is not a function. So you need to make is a list rather than function application using quote or list function i.e '(0 1 2) OR (list 0 1 2)

like image 26
Ankur Avatar answered Dec 05 '22 08:12

Ankur