Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #364: Sand fleas eating the Internet cables


comp / comp.lang.lisp / Re: Why is my local variable value accumulated?

SubjectAuthor
o Re: Why is my local variable value accumulated?B. Pym

1
Subject: Re: Why is my local variable value accumulated?
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Sun, 18 Aug 2024 16:56 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (B. Pym)
Newsgroups: comp.lang.lisp
Subject: Re: Why is my local variable value accumulated?
Date: Sun, 18 Aug 2024 16:56:54 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 55
Message-ID: <v9t94l$2f39u$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Sun, 18 Aug 2024 18:56:54 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="10aa9be4be86489a0843ffd4a9be966b";
logging-data="2592062"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18fQyNQKc3Q+weAtNefB5Ur"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:/oQleSo/o971Qjyve3zZJHH0ce4=
View all headers

> It's chapter 11 exercise 11.22 d
>
> (defun count-bases (dna)
> (let ((dnaCnt '((A 0) (T 0) (G 0) (C 0))))
> (labels ((count-element (x)
> (incf (second (assoc x dnaCnt))))
> (count-recurse (x)
> (cond ((null x) t)
> ((atom (first x)) (and (count-element (first x))
> (count-recurse (rest x))))
> (t (and (count-recurse (first x))
> (count-recurse (rest x)))))))
> (count-recurse dna)
> dnaCnt)))
>
> The code is compiled without a problem.
>
> When I run the program first time by evaluating
>
> (count-bases '((g c) (a t) (t a) (t a) (c g)))
>
> It gives the correct output:
> ((A 3) (T 3) (G 2) (C 2))
>
> However, if I run it second time,

newLISP

(define (count-bases dna , (output '()))
(dolist (s (flat dna))
(ainc! output s))
output)

(count-bases '((g c) (a t) (t a) (t a) (c g)))
===>
((t 3) (a 3) (c 2) (g 2))

Given:

(macro (ainc! Alist Key Value Function Deflt)
(local (E-Message Val Func Def)
(setq Func Function)
(if (true? Func)
(setq Val Value)
(begin (setq Func +) (setq Val (or Value 1))))
(setq Def Deflt)
(if (= nil Def) (setq Def 0))
(unless
(catch
(setf (assoc Key Alist)
(list ($it 0) (Func Val ($it 1))))
'E-Message)
(if (starts-with E-Message "ERR: no reference")
(setf Alist (cons (list Key (Func Val Def)) Alist))
(throw-error E-Message)))))

1

rocksolid light 0.9.8
clearnet tor