From: ·············@gmail.com
Subject: Could you help me to explain this piece of code
Date: 
Message-ID: <1184792583.874782.52940@e16g2000pri.googlegroups.com>
Hi everyone,

I'm a new Lisp learner. I'm doing the DNA problem: count the bases A,
T, G, and C that occur in either a single
or complement DNA (a single DNA is a list, a complement DNA a table).
My following piece of code counts the things right, but the variable
result remains and get acculumated in subsequent calls of the count-
bases function. I really don't understand why. Could you please shed
some light?

Thanks.

(defun count-bases (strand)
  (let ((result '((a 0) (t 0) ( g 0) (c 0))))
    (dolist (base strand result)
      (cond ((atom base)(incf (second (assoc base result))))
	    (t (incf (second (assoc (first base) result)))
	       (incf (second (assoc (second base) result))))))))

From: Andras Simon
Subject: Re: Could you help me to explain this piece of code
Date: 
Message-ID: <vcdejj5fljr.fsf@csusza.math.bme.hu>
·············@gmail.com writes:

> Hi everyone,
> 
> I'm a new Lisp learner. I'm doing the DNA problem: count the bases A,
> T, G, and C that occur in either a single
> or complement DNA (a single DNA is a list, a complement DNA a table).
> My following piece of code counts the things right, but the variable
> result remains and get acculumated in subsequent calls of the count-
> bases function. I really don't understand why. Could you please shed
> some light?
> 
> Thanks.
> 
> (defun count-bases (strand)
>   (let ((result '((a 0) (t 0) ( g 0) (c 0))))
                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~
should be

        (list (a 0) (t 0) ( g 0) (c 0))

http://www.lisp.org/HyperSpec/Body/speope_quote.html

"The consequences are undefined if literal objects (including quoted
objects) are destructively modified." 


>     (dolist (base strand result)
>       (cond ((atom base)(incf (second (assoc base result))))
> 	    (t (incf (second (assoc (first base) result)))
> 	       (incf (second (assoc (second base) result))))))))
From: Kent M Pitman
Subject: Re: Could you help me to explain this piece of code
Date: 
Message-ID: <u1wf51f2q.fsf@nhplace.com>
Andras Simon <······@math.bme.hu> writes:

> should be
> 
>         (list (a 0) (t 0) ( g 0) (c 0))

Uh... once you unquote the outer list, you have to think about
the inner lists.  As written here, those are function calls to
a function called A, T, etc.  See my other response to this same
poster when he posted the same question on another thread.
From: Andras Simon
Subject: Re: Could you help me to explain this piece of code
Date: 
Message-ID: <vcd644gfthw.fsf@csusza.math.bme.hu>
Kent M Pitman <······@nhplace.com> writes:

> Andras Simon <······@math.bme.hu> writes:
> 
> > should be
> > 
> >         (list (a 0) (t 0) ( g 0) (c 0))
> 
> Uh... once you unquote the outer list, you have to think about
> the inner lists.  As written here, those are function calls to
> a function called A, T, etc.  See my other response to this same
> poster when he posted the same question on another thread.

Of course you're absolutely right. I have to admit I didn't even look
at what was in that list. 

Andras
From: Daniel Janus
Subject: Re: Could you help me to explain this piece of code
Date: 
Message-ID: <slrnf9t6i2.433.przesunmalpe@students.mimuw.edu.pl>
Dnia 18.07.2007 Andras Simon <······@math.bme.hu> napisa�/a:

>>   (let ((result '((a 0) (t 0) ( g 0) (c 0))))
>                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~
> should be
>
>         (list (a 0) (t 0) ( g 0) (c 0))

Actually, it should be

   (list ('a 0) ('t 0) ('g 0) ('c 0))

-- 
Daniel 'Nathell' Janus, GG #1631668, ············@nathell.korpus.pl
   create_initial_thread(initial_function);
   lose("CATS.  CATS ARE NICE.\n");
      -- Steel Bank Common Lisp, sbcl/runtime/runtime.c:425
From: Geoffrey Summerhayes
Subject: Re: Could you help me to explain this piece of code
Date: 
Message-ID: <1184859265.921098.27800@w3g2000hsg.googlegroups.com>
On Jul 18, 6:54 pm, Daniel Janus <············@nathell.korpus.pl>
wrote:
> Dnia 18.07.2007 Andras Simon <······@math.bme.hu> napisa�/a:
>
> >>   (let ((result '((a 0) (t 0) ( g 0) (c 0))))
> >                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > should be
>
> >         (list (a 0) (t 0) ( g 0) (c 0))
>
> Actually, it should be
>
>    (list ('a 0) ('t 0) ('g 0) ('c 0))
>

(list (list 'a 0) (list 't 0) (list 'g 0) (list 'c 0))

or

(loop for x in '(a t g c) collect (list x 0))

----
Geoff
From: Andras Simon
Subject: Re: Could you help me to explain this piece of code
Date: 
Message-ID: <vcdabtsftnl.fsf@csusza.math.bme.hu>
Daniel Janus <············@nathell.korpus.pl> writes:

> Dnia 18.07.2007 Andras Simon <······@math.bme.hu> napisa�/a:
> 
> >>   (let ((result '((a 0) (t 0) ( g 0) (c 0))))
> >                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~
> > should be
> >
> >         (list (a 0) (t 0) ( g 0) (c 0))
> 
> Actually, it should be
> 
>    (list ('a 0) ('t 0) ('g 0) ('c 0))

Yes, of course! 
Thanks for the correction,

Andras
 
From: Pascal Bourguignon
Subject: Re: Could you help me to explain this piece of code
Date: 
Message-ID: <87myxsif5q.fsf@voyager.informatimago.com>
Andras Simon <······@math.bme.hu> writes:

> Daniel Janus <············@nathell.korpus.pl> writes:
>
>> Dnia 18.07.2007 Andras Simon <······@math.bme.hu> napisał/a:
>> 
>> >>   (let ((result '((a 0) (t 0) ( g 0) (c 0))))
>> >                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~
>> > should be
>> >
>> >         (list (a 0) (t 0) ( g 0) (c 0))
>> 
>> Actually, it should be
>> 
>>    (list ('a 0) ('t 0) ('g 0) ('c 0))
>
> Yes, of course! 
> Thanks for the correction,

Of course not.

((QUOTE A) 0) is is even more invalid than (A 0).

(A 0) could be a valid form.  ((QUOTE A) 0) never.



You could use:

   (mapcar (lambda (base) (list base 0)) (quote (a t g c)))

or:

    (list (list (quote a) 0)
          (list (quote t) 0)
          (list (quote c) 0)
          (list (quote g) 0))

or:

   (copy-tree (quote ((a 0) (t 0) (c 0) (g 0))))


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

In a World without Walls and Fences, 
who needs Windows and Gates?
From: ·············@gmail.com
Subject: Re: Could you help me to explain this piece of code
Date: 
Message-ID: <1184854652.297751.47990@m37g2000prh.googlegroups.com>
On Jul 18, 3:29 pm, Andras Simon <······@math.bme.hu> wrote:
> ·············@gmail.com writes:
> > Hi everyone,
>
> > I'm a new Lisp learner. I'm doing the DNA problem: count the bases A,
> > T, G, and C that occur in either a single
> > or complement DNA (a single DNA is a list, a complement DNA a table).
> > My following piece of code counts the things right, but the variable
> > result remains and get acculumated in subsequent calls of the count-
> > bases function. I really don't understand why. Could you please shed
> > some light?
>
> > Thanks.
>
> > (defun count-bases (strand)
> >   (let ((result '((a 0) (t 0) ( g 0) (c 0))))
>
>                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~
> should be
>
>         (list (a 0) (t 0) ( g 0) (c 0))
>
> http://www.lisp.org/HyperSpec/Body/speope_quote.html
>
> "The consequences are undefined if literal objects (including quoted
> objects) are destructively modified."
>
>
>
> >     (dolist (base strand result)
> >       (cond ((atom base)(incf (second (assoc base result))))
> >        (t (incf (second (assoc (first base) result)))
> >           (incf (second (assoc (second base) result))))))))- Hide quoted text -
>
> - Show quoted text -

Could you explain why it must be that way?

Thanks.

Mark.
From: Pascal Bourguignon
Subject: Re: Could you help me to explain this piece of code
Date: 
Message-ID: <87fy3kiexe.fsf@voyager.informatimago.com>
·············@gmail.com writes:
>> http://www.lisp.org/HyperSpec/Body/speope_quote.html
>>
>> "The consequences are undefined if literal objects (including quoted
>> objects) are destructively modified."
>> [...]
> Could you explain why it must be that way?

It's not that it MUST be that way, it's that it MAY be that way.  So,
by Murphy's Law, it WILL be that way when it will be the worse for
you.


The CL standard allows implementations to compile programs and run
them from RAM or from ROM (eg in your washmaschine, or in a space
sonde).

If (let ((x (quote (a 0)))) (incf (second x))) is compiled into RAM,
then it's probable that the literal object will be modified.  If it's
compiled into ROM, then the literal object won't be able to be
modified.  Therefore you'll get two different results.  That's why
what you get is unspecified.  Note that on some systems trying to
write into ROM may signal an exception, it's not necessarily a NOP.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
Until real software engineering is developed, the next best practice
is to develop with a dynamic system that has extreme late binding in
all aspects. The first system to really do this in an important way
is Lisp. -- Alan Kay
From: Pascal Costanza
Subject: Re: Could you help me to explain this piece of code
Date: 
Message-ID: <5g9fblF3gae9hU1@mid.individual.net>
Pascal Bourguignon wrote:
> ·············@gmail.com writes:
>>> http://www.lisp.org/HyperSpec/Body/speope_quote.html
>>>
>>> "The consequences are undefined if literal objects (including quoted
>>> objects) are destructively modified."
>>> [...]
>> Could you explain why it must be that way?
> 
> It's not that it MUST be that way, it's that it MAY be that way. 

Literal objects are part of your source code. An attempt to 
destructively modify literal objects is effectively an attempt to 
destructively modify the source code of a program from within that 
program itself. That's ugly and unreliable (should the text buffer 
presenting the source code on the screen be updated in such a case?), 
and there are better and more principled ways to achieve the desired 
effects. In other words, there is no loss in expressiveness when making 
that restriction.


Pascal

-- 
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/