I wrote my silly function which returns a list without the last element in common lisp. Is there any more elegant solution to this problem?
Here's my code:
(defun list-without-last (l)
(if (> (length (rest l)) 0)
(append (list (first l)) (list-without-last (rest l)))
nil))
Short and simple, just like Lisp. Here is the magic stuff:
(defun without-last(l)
(reverse (cdr (reverse l)))
)
Your function has two problems:
you are using LENGTH. LENGTH has to scan the whole list.
you are using APPEND. Try to use CONS. CONS is simpler.
Common Lisp also already provides this function. It is called BUTLAST.
In real code we also would not use recursion. The stack size would limit the length of lists we can process.
An iterative version using the LOOP
macro:
CL-USER> (defun my-butlast (list)
(loop for l on list
while (rest l)
collect (first l)))
MY-BUTLAST
CL-USER> (compile 'my-butlast)
MY-BUTLAST
NIL
NIL
CL-USER> (my-butlast '(1 2 3 4 5))
(1 2 3 4)
CL-USER> (my-butlast '(1))
NIL
CL-USER> (my-butlast '(1 2))
(1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With