progn

When you want to have a sequence of expressions a, b, c to evaluate, you need to put them in a (progn a b c) form. But here you had them wrapped in a list of forms: when you evaluate (a b c) in a normal evaluation context, ie. not in macros, special forms, …, a is expected to be a function being called with arguments b and c. That’s why you have that specific error: (push ...) is indeed not a function name or a lambda expression.

Undefined variables

You are calling setq on symbols that are not bound to known variables, at least in your function. Most implementations then treat the assignment as an assignment to symbol-value of the symbol, even though the behaviour is unspecified. You should rather declare them in your function by having a let binding:

(let ((w (length list))
      (... ...))
  ...)

By the way, you compute the length of the list twice in this function, the length could be computed once only, and in fact you only need to know if the list has one element and no list in its cdr: this is a constant-time computation, unlike list-length which needs to traverse the whole list.

variable names, case

Be careful about how you name your variables, and please stick to the Lisp conventions by using hyphen-separated-words instead of a mix of upper and lower case.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top