Sujet : Re: Multivalue tail recursion?
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lisp comp.lang.schemeDate : 03. Jul 2025, 17:42:37
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <1046bts$8sho$1@dont-email.me>
User-Agent : XanaNews/1.18.1.6
Ken Tilton wrote:
Anyway, I whipped one up in a few lines that not only pulls n items
off the list, but it returns the remaining items as well. Let me know
what you think. I was kind of surprised that I was able to do it
without the use of an accumulator parameter.
>
(defun first-n (n list)
"Splits the list after the first n elements"
(if (= 0 n)
(values '() list)
(multiple-value-bind (from-front rest) (first-n (1- n) (rest
list))
(values (cons (first list) from-front) rest))))
>
>
I would probably do something like this instead:
>
(defun first-n (n list)
(loop for i below n for (a . d) on list
collect a into x
finally (return (values x d))))
Niiiiiiice.
Gauche Scheme:
(define (first-n n xs . ys)
(dotimes (n) (push! ys (pop! xs)))
(values (reverse ys) xs))