Since you are calling the function f recursively, the use of setq (as noted the following expression) is unnecessary, as the values of the symbols l and n are passed as arguments to the f function.
- (f (setq q [highlight](setq[/highlight] l (cdr l))) [highlight](setq[/highlight] n (1- n)))
Also, by renaming the parameters of the f function, you could remove the need for the extra 'state' variables k & q, e.g.:
- (defun gn ( l n / f g ) (defun f ( a b ) (if (and a (< 0 b)) (cons (car a) (f (setq l (cdr a)) (1- b))) ) ) (defun g ( l n ) (if l (cons (f l n) (g l n)))) (g l n))
However, note further that the function g is also superfluous since it is identical to the function in which it is defined:
- (defun gn ( l n / f ) (defun f ( a b ) (if (and a (< 0 b)) (cons (car a) (f (setq l (cdr a)) (1- b))) ) ) (if l (cons (f l n) (gn l n))))
|