Reputation: 6831
I now have a problem on using "reduce" to implement my own version of copy-list. This is what I have done:
(defun my-copy-list (lst)
(reduce #'(lambda (x y)
(cons x y))
lst :initial-value nil :from-end t))
However, my teacher said there is no need to use that lambda, I am confused on this. How may we achieve the same functionality without using that lambda (but must use 'reduce'). Thanks a lot.
Upvotes: 3
Views: 398
Reputation: 72015
What your teacher means is that you're defining this function
(lambda (x y) (cons x y))
But there's already a function that exists to do that -- cons
itself. So instead of passing your lambda as an argument to reduce
, you could just pass cons
.
Upvotes: 15
Reputation: 91502
this is what cons does: it takes two values and pairs them.
this is what (lambda (x y) (cons x y))
does: it takes two values and pairs them.
Upvotes: 2