Sujet : Re: DEFUN list argument
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lisp comp.lang.schemeDate : 29. Sep 2024, 01:52:08
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vda8bn$1f4vl$1@dont-email.me>
User-Agent : XanaNews/1.18.1.6
(defun list-of-length (n list)
"Predicate which tests whether LIST is a list
of length N."
(loop for i from 0 below n
when (null list) do (return nil)
do (pop list)
finally (return (null list))))
Instead of using a macro whose source measures 60 kilobytes,
we can make it shorter by simply using recursion. Of course,
that's not possible in CL since it's not a Lispy language and
doesn't offer tail-call optimization. (Lispy languages
encourage recursive solutions.)
Scheme:
(define (list-of-length? n lst)
(cond ((zero? n) (null? lst))
((null? lst) #f)
(#t (list-of-length? (- n 1) (cdr lst)))))