functional-koans / clojure-koans

A set of exercises for learning Clojure
3.74k stars 2.14k forks source link

13_creating_functions.clj (has a bug?) #120

Closed IrSent closed 6 years ago

IrSent commented 7 years ago
"Don't forget: first things first"
  (= [__ __ __ __]
       (let [ab-adder (partial concat [:a :b])]
       (ab-adder [__ __])))

Using http://clojurescript.net/ REPL that expression returns an Array (:a :b __ __), and not a Vector [:a :b __ __] as it is stated in the koan (ab-adder [__ __ __ __])

clojure.core/concat documentation

It would be a Vector if we add into []:

(into [] (concat [:a :b] ["a" "b"]))
trptcolin commented 7 years ago

Right, so this is a pretty common idiom in Clojure. When we compare using =, [:a, :b :c] is the same as '(:a :b :c), which is the same as (cons :a '(:b :c)), etc. - even though those are all different concrete types (clojure.lang.PersistentVector, clojure.lang.PersistentList, and clojure.lang.Cons, respectively).

The equality comparison doesn't care about the type, just the values contained in the sequential collection. And vector is a commonly used literal, in part since unlike the list literal, you don't have to worry about quoting.