Path: news.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: "B. Pym" Newsgroups: comp.lang.lisp,comp.lang.scheme Subject: Re: Ex. 3.5 in ACL Date: Wed, 9 Jul 2025 14:45:44 -0000 (UTC) Organization: A noiseless patient Spider Lines: 57 Message-ID: <104lvam$9get$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Injection-Date: Wed, 09 Jul 2025 16:45:44 +0200 (CEST) Injection-Info: dont-email.me; posting-host="cf1a52de3d14298e4fc9081e3617a27e"; logging-data="311773"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18We6zgBwBlY8W5BTfVB/ky" User-Agent: XanaNews/1.18.1.6 Cancel-Lock: sha1:iLrYa2HRU98oKwz6ZkKCEI76S5o= Zachary Beane wrote: > > I am just trying to solve Ex 3.5 in Graham's ANSI Common Lisp book. I am > > reading it on my own and not as part of a university course. The task is: > > > > define a function pos+, that takes a list as param and returns a list > > that adds the position of each element of the list to the element's value. > > Thus: > > (pos+ '(7 5 1 4)) > > returns: > > (7 6 3 7) > ...but it's also easy with DO: > > (defun do-pos+ (orig-list) > (do ((i 0 (1+ i)) > (list orig-list (cdr list)) > (new-list nil (cons (+ (car list) i) new-list))) > ((endp list) (nreverse new-list)))) (define (do-pos+ orig-list) (do_ ((i 0 (+ 1 i)) (x :in orig-list) (result :collect (+ i x))) (#f @ result))) (do-pos+ '(200 300 400)) ===> (200 301 402) Given: (define-syntax do_-aux (syntax-rules ( <> @ :in :collect ) [ (do_-aux ((x what <>) more ...) (seen ...) stuff ...) (do_-aux (more ...) (seen ... (x what what)) stuff ...) ] [ (do_-aux ((x :in seq) more ...) seen (lets ...) (bool z ...) stuff ...) (do_-aux ((x (and (pair? the-list) (car the-list)) <>) more ...) seen (lets ... (the-list seq)) ((or (null? the-list) (begin (pop! the-list) #f) bool) z ...) stuff ...) ] [ (do_-aux ((accum :collect x) more ...) stuff ...) (do_-aux ((accum '() (cons x accum)) more ...) stuff ...) ] [ (do_-aux (spec more ...) (seen ...) stuff ...) (do_-aux (more ...) (seen ... spec) stuff ...) ] [ (do_-aux () seen lets (bool y ... @ result) stuff ...) (do_-aux () seen lets (bool y ... (reverse result)) stuff ...) ] [ (do_-aux () seen (lets ...) more ...) (let (lets ...) (do seen more ...)) ] )) (define-syntax do_ (syntax-rules () [ (do_ specs more ...) (do_-aux specs () () more ...) ] ))