0

I tried to use this code, but it returns (k k k k k)

(mapcar (lambda (x) 'cons x '(k))
        '(a b c d e))

What am i doing wrong here? Why doesn't cons work?

  • 2
    n.b. (lambda (x) 'cons x '(k)) is a function which returns '(k) (after a couple of pointless evaluations). That mapcar therefore should be returning ((k) (k) (k) (k) (k)) which is different to what you've said. – phils Jan 29 at 1:07
  • 2
    Also, be careful with things like '(k) in that context -- that's the exact same list being placed in each cell of that output. If you were to setcar any one of those (k) instances to, say, j, then not only would the entire list become ((j) (j) (j) (j) (j)), but the lambda function itself would now be returning that same'(j) as well (unless it was re-read). You frequently want (list 'k) rather than '(k). – phils Jan 29 at 1:13
6

Just syntax error. To create your alist, use to following:

(mapcar (lambda (x) (cons x 'k))
        '(a b c d e))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.