lisp - Clojure Koan section 8, #5 -
why valid:
(= '(:anything :goes :here) (filter (fn [x] true) '(:anything :goes :here))) but not this?
(= (:anything :goes :here) (filter (fn [x] true) '(:anything :goes :here))) or
(= (:anything :goes :here) (filter (fn [x] true) (:anything :goes :here))) or
(= '(:anything :goes :here) (filter (fn [x] true) (:anything :goes :here))) is there particular reason second arg filter quoted list rather plain list?
user=> (filter (fn [x] true) (:abc :def :ghi)) illegalargumentexception don't know how create iseq from: clojure.lang.keyword clojure.lang.rt.seqfrom (rt.java:505) as matter of fact, still unsure when list function call. it's related quoting seems. "plain lists" required quoted unless empty lists?
when list evaluated, first element assumed function (or macro or special form).
when first element of list function, arguments first evaluated, , function applied resulting values.
if first element of list macro or special form, each of arguments may or may not evaluated depending on macro/special form.
clojure evaluate list first element keyword trying find keyword key in map given argument keyword function, , return corresponding value, otherwise return next argument (if given). (:anything :goes: :here) return here.
' read macro puts it's argument quote special form. i.e. 'anything =>(quote anything)
in case:
when = evaluated, values of (:anything :goes: here) and/or '(:anything goes here) have evaluated. evaluation of first 1 result in :here. (in other lisps result in error). '(:anything :goes :here), short form of (quote (:anything :goes :here)), , quote special form returns it's arguments unevaluated, resulting in list (:anything :goes: here) passed = or filter without further evaluation.
what's happening in each of cases then:
(= '(:anything :goes :here) (filter (fn [x] true) '(:anything :goes :here))) =is comparing (:anything :goes :here)to (:anything :goes :here), resulting in true
(= (:anything :goes :here) (filter (fn [x] true) '(:anything :goes :here))) :here compared (:anything :goes :here), resulting in false
(= (:anything :goes :here) (filter (fn [x] true) (:anything :goes :here))) (= '(:anything :goes :here) (filter (fn [x] true) (:anything :goes :here))) in both of these, filter applied single keyword :here, resulting in error.
Comments
Post a Comment