Simple Lisp Question

I now have a problem using reduce to implement my own version of the copy list. This is what I did:

(defun my-copy-list (lst)  
  (reduce #'(lambda (x y)  
              (cons x y)) 
          lst :initial-value nil :from-end t))

However, my teacher said that there is no need to use this lambda, I am confused by this. How can we achieve the same functionality without using this lambda (but should use "reduce"). Many thanks.

+3
source share
2 answers

What your teacher means is that you define this function

(lambda (x y) (cons x y))

But there already exists a function that exists for this - cons. So instead of passing lambda as an argument, reduceyou can just pass cons.

+15
source

here's what the minus does: it takes two values ​​and pairs of them.

this is what it does (lambda (x y) (cons x y)) : it takes two values ​​and pairs of them.

+2
source

Source: https://habr.com/ru/post/1723717/


All Articles