Kevin
Kevin

Reputation: 6831

A simple Lisp question

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

Answers (2)

mqp
mqp

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

Jimmy
Jimmy

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

Related Questions