jppower175
jppower175

Reputation: 425

eLisp recursive function

New to Lisp. Trying to pass a list into a recursive function and do something with the first item in the list each time. This is the function so far:

(setq colors '(red blue green yellow orange pink purple))

(defun my-function (x)
  (if (> (length x) 0)
      (let ((c  (car x))
            c)
        (my-function x))))

Keep getting an error saying x is a void element. Not sure what to do.

Upvotes: 0

Views: 324

Answers (1)

phils
phils

Reputation: 73246

If I reformat your function, maybe you can see what you're doing wrong:

(defun my-function (x)
  (if (> (length x) 0)  ; do nothing if list is empty
      (let ((c (car x)) ; bind c to (car x)
            c)          ; bind c to nil instead
                        ; c is never used
        (my-function x)))) ; recursively call function
                           ; with unmodified x
                           ; until the stack is blown

Keep getting an error saying x is a void element.

I imagine you're calling (my-function x) with an undefined x instead of passing it your colors list with (my-function colors), but that's certainly not your only problem.

Upvotes: 4

Related Questions