magirtopcu
magirtopcu

Reputation: 1194

Simple clojure : Why output of different expressions are same?

(map (comp #(- 9  %) *) [2 4 6] [0 0 0])

output : (9 9 9)

(map (comp #(+ 9  %) *) [2 4 6] [0 0 0])

output : (9 9 9)

Why these give same output? #(- 9 %) and #(+ 9 %) are different.

Upvotes: 0

Views: 78

Answers (2)

Thumbnail
Thumbnail

Reputation: 13483

In general

(map (comp f g) s t)

... can be refactored as

(map f (map g s t))

In this case,

(map (comp #(- 9  %) *) [2 4 6] [0 0 0])

... becomes

(map #(- 9  %) (map * [2 4 6] [0 0 0]))

... which reduces to

(map #(- 9  %) '(0 0 0))

So changing - to + makes no difference.


The above is essentially an explanation of cfrick's answer.

Upvotes: 3

cfrick
cfrick

Reputation: 37073

You are mulitplying each number with zero:

(map * [2 4 6] [0 0 0])
;; -> (0 0 0)

So what remains: 9-0 == 9+0

Upvotes: 8

Related Questions