From: Wayne
Subject: "Do" problem?
Date: 
Message-ID: <8po114$i8e$1@mozo.cc.purdue.edu>
Can anyone tell me how to deal with this program? I want to sum up 1 to 5
with "do" function.

(defun wc (x)
    (do ((result 0 (+ (first x) result))
                       (setf x (rest x)))
                    ((endp x) result)
                     ))

[2] USER(5): (wc '(1 2 3 4 5))

It never stops. Why?

From: Thomas A. Russ
Subject: Re: "Do" problem?
Date: 
Message-ID: <ymik8cgtei1.fsf@sevak.isi.edu>
"Wayne" <··@ecn.purdue.edu> writes:

> 
> Can anyone tell me how to deal with this program? I want to sum up 1 to 5
> with "do" function.
> 
> (defun wc (x)
>     (do ((result 0 (+ (first x) result))
>          (setf x (rest x)))
>         ((endp x) result)
>         ))
> 
> [2] USER(5): (wc '(1 2 3 4 5))
> 
> It never stops. Why?

Because you are not executing the code that you think you are
executing.  The DO form allows you to step multiple variables each time
around the loop, and that is what you are doing.  Hint:  you have a
variable named "setf" in your loop.

The body of your DO doesn't have any code.

Second hint:  Add the following code to the body of the do loop:

  (format t "X = ~S   SETF = ~S~%" x setf)

and you should get more help for debugging your program.


-- 
Thomas A. Russ,  USC/Information Sciences Institute          ···@isi.edu    
From: Wayne
Subject: Re: "Do" problem?
Date: 
Message-ID: <8poj4j$ou4$1@mozo.cc.purdue.edu>
I changed it to
(defun wc (x)
    (do ((result 0 (+ (first x) result))
           (setf x (rest x)))
          (format t "X = ~S   SETF = ~S~%" x setf)
        ((endp x) result)
                     ))
But it returned me,
Error: Attempt to take the value of the unbound variable `FORMAT'.
  [condition type: UNBOUND-VARIABLE]


"Thomas A. Russ" <···@sevak.isi.edu> ���g��l��
····················@sevak.isi.edu...
> "Wayne" <··@ecn.purdue.edu> writes:
>
> >
> > Can anyone tell me how to deal with this program? I want to sum up 1 to
5
> > with "do" function.
> >
> > (defun wc (x)
> >     (do ((result 0 (+ (first x) result))
> >          (setf x (rest x)))
> >         ((endp x) result)
> >         ))
> >
> > [2] USER(5): (wc '(1 2 3 4 5))
> >
> > It never stops. Why?
>
> Because you are not executing the code that you think you are
> executing.  The DO form allows you to step multiple variables each time
> around the loop, and that is what you are doing.  Hint:  you have a
> variable named "setf" in your loop.
>
> The body of your DO doesn't have any code.
>
> Second hint:  Add the following code to the body of the do loop:
>
>   (format t "X = ~S   SETF = ~S~%" x setf)
>
> and you should get more help for debugging your program.
>
>
> --
> Thomas A. Russ,  USC/Information Sciences Institute          ···@isi.edu
From: Barry Margolin
Subject: Re: "Do" problem?
Date: 
Message-ID: <dDQv5.71$tQ5.2630@burlma1-snr2>
In article <············@mozo.cc.purdue.edu>, Wayne <··@ecn.purdue.edu> wrote:
>I changed it to
>(defun wc (x)
>    (do ((result 0 (+ (first x) result))
>           (setf x (rest x)))
>          (format t "X = ~S   SETF = ~S~%" x setf)
>        ((endp x) result)
>                     ))
>But it returned me,
>Error: Attempt to take the value of the unbound variable `FORMAT'.
>  [condition type: UNBOUND-VARIABLE]

You have FORMAT in the place where the end-test should be.  So it's trying
to use it as a variable, and if it evaluates to true the iteration will
terminate.

It helps if you indent properly:

    (do ((result 0 (+ (first x) result))  ; iterate variable named RESULT
         (setf x (rest x)))               ; iterate variable named SETF
        (format t "X = ~S   SETF = ~S~%" x setf) ; end-test is FORMAT
      ((endp x) result))                  ; this is the body of the loop

I think you need to examine the syntax of DO in detail in the hyperspec or
CLTL.

-- 
Barry Margolin, ······@genuity.net
Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: Wayne
Subject: Re: "Do" problem?
Date: 
Message-ID: <8pou05$stk$1@mozo.cc.purdue.edu>
I got it!

(defun wc (x)
    (do ((result 0 (+ (first x) result))  ; iterate variable named RESULT
         (x x (rest x)))               ; iterate variable named SETF
      ((endp x) result)))                 ; this is the body of the loop

 Thanks your advise.
From: Barry Margolin
Subject: Re: "Do" problem?
Date: 
Message-ID: <GfTv5.77$tQ5.2899@burlma1-snr2>
In article <············@mozo.cc.purdue.edu>, Wayne <··@ecn.purdue.edu> wrote:
>I got it!
>
>(defun wc (x)
>    (do ((result 0 (+ (first x) result))  ; iterate variable named RESULT
>         (x x (rest x)))               ; iterate variable named SETF
>      ((endp x) result)))                 ; this is the body of the loop

Don't forget to update the comments when you update the code!

-- 
Barry Margolin, ······@genuity.net
Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
From: Wayne
Subject: Re: "Do" problem? Thanks
Date: 
Message-ID: <8pp9g9$3aj$1@mozo.cc.purdue.edu>
Thanks for your remind.
"Barry Margolin" <······@genuity.net> ���g��l��
······················@burlma1-snr2...
> In article <············@mozo.cc.purdue.edu>, Wayne <··@ecn.purdue.edu>
wrote:
> >I got it!
> >
> >(defun wc (x)
> >    (do ((result 0 (+ (first x) result))  ; iterate variable named RESULT
> >         (x x (rest x)))               ; iterate variable named SETF
> >      ((endp x) result)))                 ; this is the body of the loop
>
> Don't forget to update the comments when you update the code!
>
> --
> Barry Margolin, ······@genuity.net
> Genuity, Burlington, MA
> *** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to
newsgroups.
> Please DON'T copy followups to me -- I'll assume it wasn't posted to the
group.
From: Rainer Joswig
Subject: Re: "Do" problem?
Date: 
Message-ID: <joswig-40DE91.16144313092000@news.is-europe.net>
In article <············@mozo.cc.purdue.edu>, "Wayne" 
<··@ecn.purdue.edu> wrote:

> Can anyone tell me how to deal with this program? I want to sum up 1 to 5
> with "do" function.
> 
> (defun wc (x)
>     (do ((result 0 (+ (first x) result))
>                        (setf x (rest x)))
>                     ((endp x) result)
>                      ))
> 
> [2] USER(5): (wc '(1 2 3 4 5))
> 
> It never stops. Why?
> 
> 
> 

The SETF form is at a wrong place. See
http://www.xanalys.com/software_tools/reference/HyperSpec/Body/mac_docm_dost.html
for more info about DO and DO* .

If you want to add up all numbers of a list:

(reduce #'+ '(1 2 3 4 5))

or

(let ((result 0))
  (dolist (element '(1 2 3 4 5) result)
    (incf result element)))

or

(loop for element in '(1 2 3 4 5)
      sum element)


If you want to count the number of elements:

(length '(1 2 3 4 5))

-- 
Rainer Joswig, Hamburg, Germany
Email: ·············@corporate-world.lisp.de
Web: http://corporate-world.lisp.de/