Reputation: 585
I need to implement the function which creates a list from arguments that are passed to the function.
Here's my code:
(defun lstbuilder (&rest args)
(if (eq (car args) NIL)
NIL
(cons (car args)
(lstbuilder (cdr args)))))
This function doesn't work correctly. Results:
(lstbuilder 'a 'b 'c 'd 'e) ;expected (a b c d e)
(a (b c d e)) ;result
Upvotes: 3
Views: 121
Reputation: 139411
Style
Please use standard Lisp formatting. Use an editor will help you to indent Lisp code.
Don't put a parenthesis alone on a line. This just wastes space without benefit.
Longer names get a -
between the words: list-builder
.
Don't use car
and cdr
for list operations. Use first
and rest
.
The end of list test is endp
.
Example:
(defun list-builder (&rest args)
(if (endp args)
nil
(cons (first args)
(apply #'list-builder (rest args)))))
Since the args
variable is already a list, we can just copy it:
(defun list-builder (&rest args)
(copy-list args))
Or we can just reuse the list
function, which already creates a list of its args:
(setf (symbol-function 'list-builder)
#'list)
Upvotes: 5
Reputation: 223213
You need to use (apply #'lstbuilder (cdr args))
in order to "splat" the list's contents as the function call arguments.
Upvotes: 4