1

I want to implement filter function that would filter a list based on a condition

(defun filter (func xs)                                                          
  (mapcan                                                                        
    (lambda (x)                                                                  
      (when (func x) (list x))) xs ))                                            

but I get an error:

*** - EVAL: undefined function FUNC

I thought that lambda should see func. How to pass func to lambda correctly?

I use CLISP.

Community
  • 1
  • 1
Jakub M.
  • 32,471
  • 48
  • 110
  • 179

1 Answers1

6

You want

(when (funcall func x) (list x))

instead of

(when (func x) (list x))

More information about function vs. variable namespace:

Community
  • 1
  • 1
finnw
  • 47,861
  • 24
  • 143
  • 221
  • Just mentioning that `(func x)` tries to evaluate a function called `func` whereas `(funcall func x)` call a function called `func` with `x` as its argument. – George Ogden Aug 18 '22 at 11:17