Search code examples
listlambdalispelispapply

elisp how to apply a lambda to a list?


What I'm trying to do seems simple enough, and whatever's wrong must be a really dumb mistake, since I couldn't find other people getting the same error. I just want to apply a lambda to a list - the lambda here isn't what I actually want to do, but it gives the same error.

(apply
(lambda (arg)
  (+ 5 arg)
)
(list 2 3 4)
)

When I try to run this, it tells me that I'm passing the lambda an invalid number of arguments. Do you have any advice?


Solution

  • apply calls the function once, passing it the list you've given as the arguments. I think you instead want to use mapcar:

    M-: (mapcar (lambda (arg) (+ 5 arg)) (list 2 3 4)) RET
    

    will return the list (7 8 9).