From: ············@gmail.com
Subject: do inside a do
Date: 
Message-ID: <1164666273.525619.277000@l12g2000cwl.googlegroups.com>
I was trying to create a function that given two initial parameters, it
would iterate through a specific part of an array. I'm tryng to create
this with two do's one inside the other, but i'm not getting results.
here's a sample of my code in side the fubction:

(do ((n sl (1+ n)))
        ((= n el) count)
      (do ((k sc (1+ k)))
          ((= k ec) ))
      (if (eq (give-numnber array n k)
              number)
          (incf count)))
    count))
the compiler says K is neither declared nor bound,
 |   it will be treated as if it were declared SPECIAL.

what i'm a doing wrong, it is possible to accomplish  this using do.
any sugestions, comments...
thank you.

From: Kaz Kylheku
Subject: Re: doh inside a doh
Date: 
Message-ID: <1164667580.384052.147290@h54g2000cwb.googlegroups.com>
············@gmail.com wrote:
> I was trying to create a function that given two initial parameters, it
> would iterate through a specific part of an array. I'm tryng to create
> this with two do's one inside the other, but i'm not getting results.
> here's a sample of my code in side the fubction:
>
> (do ((n sl (1+ n)))
>         ((= n el) count)
>       (do ((k sc (1+ k)))
>           ((= k ec) ))
>       (if (eq (give-numnber array n k)

EQ???

These are number you are comparing, right? Then you want EQL or =,
depending on whether you want 1.0 to be equal to 1.

>               number)
>           (incf count)))
>     count))

(incf count
  (loop for n from sl to el
        summing
         (loop for k from sc to ec
                counting (eql (give-numnber array n k) number))))

> the compiler says K is neither declared nor bound,
>  |   it will be treated as if it were declared SPECIAL.
>
> what i'm a doing wrong,

Mismanaging your parenthesis nesting, most likely.

> it is possible to accomplish  this using do.

It is. But it's harder to read than if it's accomplished with the LOOP
macro.
From: Pascal Costanza
Subject: Re: do inside a do
Date: 
Message-ID: <4t187dF116ns4U1@mid.individual.net>
············@gmail.com wrote:
> I was trying to create a function that given two initial parameters, it
> would iterate through a specific part of an array. I'm tryng to create
> this with two do's one inside the other, but i'm not getting results.
> here's a sample of my code in side the fubction:
> 
> (do ((n sl (1+ n)))
>         ((= n el) count)
>       (do ((k sc (1+ k)))
>           ((= k ec) ))
>       (if (eq (give-numnber array n k)
>               number)
>           (incf count)))
>     count))
> the compiler says K is neither declared nor bound,
>  |   it will be treated as if it were declared SPECIAL.
> 
> what i'm a doing wrong, it is possible to accomplish  this using do.
> any sugestions, comments...

In my very subjective personal opinion, DO is quite an ugly construct 
and very hard to read. It's a good construct for generated code, but not 
so useful for writing user-level code.

It's nicer to use LOOP here, and the translation from DO to LOOP is 
straightforward:

(loop for n = s1 then (1+ n)
       until (= n el) do
       (loop for k = sc then (1+ k)
             until (= k ec)
             when (eql (give-number array n k) number)
             do (incf count))
       finally (return count))


Note: Never compare numbers with EQ, always use EQL for numbers. To be 
on the safe side, generally prefer EQL over EQ. EQ on numbers and on 
characters is underspecified.


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/
From: Pascal Costanza
Subject: Re: do inside a do
Date: 
Message-ID: <4t18c2F11hganU1@mid.individual.net>
Pascal Costanza wrote:
> ············@gmail.com wrote:
>> I was trying to create a function that given two initial parameters, it
>> would iterate through a specific part of an array. I'm tryng to create
>> this with two do's one inside the other, but i'm not getting results.
>> here's a sample of my code in side the fubction:
>>
>> (do ((n sl (1+ n)))
>>         ((= n el) count)
>>       (do ((k sc (1+ k)))
>>           ((= k ec) ))
>>       (if (eq (give-numnber array n k)
>>               number)
>>           (incf count)))
>>     count))
>> the compiler says K is neither declared nor bound,
>>  |   it will be treated as if it were declared SPECIAL.
>>
>> what i'm a doing wrong, it is possible to accomplish  this using do.
>> any sugestions, comments...
> 
> In my very subjective personal opinion, DO is quite an ugly construct 
> and very hard to read. It's a good construct for generated code, but not 
> so useful for writing user-level code.
> 
> It's nicer to use LOOP here, and the translation from DO to LOOP is 
> straightforward:
> 
> (loop for n = s1 then (1+ n)
>       until (= n el) do
>       (loop for k = sc then (1+ k)
>             until (= k ec)
>             when (eql (give-number array n k) number)
>             do (incf count))
>       finally (return count))

My loop version was a mechanical translation from your do version. Kaz's 
version is even better. ;)

> Note: Never compare numbers with EQ, always use EQL for numbers. To be 
> on the safe side, generally prefer EQL over EQ. EQ on numbers and on 
> characters is underspecified.


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/
From: Harald Hanche-Olsen
Subject: Re: do inside a do
Date: 
Message-ID: <pcoslg4mq73.fsf@shuttle.math.ntnu.no>
+ ············@gmail.com:

| what i'm a doing wrong

What you're /really/ doing wrong is not using an editor that will indent
your code for you.  If had, and if you had used it properly, you would
have seen

(do ((n sl (1+ n)))
    ((= n el) count)
  (do ((k sc (1+ k)))
      ((= k ec)))
  (if (eq (give-numnber array n k)
	  number)
      (incf count)))

from which your mistake is immediately apparent: The final IF is at
the same level as the inner DO, instead of inside it.

-- 
* Harald Hanche-Olsen     <URL:http://www.math.ntnu.no/~hanche/>
- It is undesirable to believe a proposition
  when there is no ground whatsoever for supposing it is true.
  -- Bertrand Russell
From: Sidney Markowitz
Subject: Re: do inside a do
Date: 
Message-ID: <456b66de$0$82611$742ec2ed@news.sonic.net>
············@gmail.com wrote, On 28/11/06 11:24 AM:
> (do ((n sl (1+ n)))
>         ((= n el) count)
>       (do ((k sc (1+ k)))
>           ((= k ec) ))
                       ^

You have an extra close paren there, so k really is not known past that
line.

-- 
    Sidney Markowitz
    http://www.sidney.com
From: ············@gmail.com
Subject: Re: do inside a do
Date: 
Message-ID: <1164667136.406935.14050@n67g2000cwd.googlegroups.com>
thanks for the quick reply.