I need help decrypting this clojure code

(map drop '(0 1) '((0 1 2 3) (1 2 3))) 

Answer: ((0 1 2 3) (2 3))

Can someone explain what is happening? I can not decrypt this code ?!

Thanks!

+4
source share
2 answers

Clojure map can take several sequences after the operand of the function, and it buttons one element for each seq. When the first seq is exhausted, the card ends.

In your form, you give map two seqs: '(0 1) and '((0 1 2 3) (1 2 3)) , which have two elements. Thus, the code describes two calls to drop :

 (drop 0 '(0 1 2 3)) ; => '(0 1 2 3) (drop 1 '(1 2 3)) ; => '(2 3) 

Hope this document helps clear this out (my attention):

 clojure.core/map ([f coll] [f c1 c2] [f c1 c2 c3] [f c1 c2 c3 & colls]) 

Returns a lazy sequence consisting of the result of applying f to the set of first elements of each column, and then applying f to the set of second elements in each column until none of them are depleted. Any other elements in other calls are ignored. Function
f must accept number-of-coll arguments.

+9
source

Please note that the Clojure map is a fairly flexible function, as it can do several things where you need different functions in other languages.

For example, it performs all three Haskell functions:

 map :: (a -> b) -> [a] -> [b] zip :: [a] -> [b] -> [(a, b)] zipWith :: (a -> b -> c) -> [a] -> [b] -> [c] 

Where

 zip = zipWith (,) 

So, in Clojure, you use map to do two different things:

a) Convert the sequence in order to a sequence of the same length. This is what is called a "map" in Haskell and other languages.

b) Replace two or more sequences together with one element, creating a sequence that reaches the shortest input sequence. This is called "zipWith" in Haskell.

The attached function should take as many parameters as there are input sequences, and returns the elements that should be included in the output sequence.

The code you specified uses map in the second function. It extracts 0 elements from (0 1 2 3) and 1 element from (1 2 3) . Results that (0 1 2 3) and (2 3) fall into the sequence of results.

+2
source

All Articles