From: GP lisper
Subject: Skipping lines in text file
Date: 
Message-ID: <1118298603.907f16e465d11543587a9648ccbee7ab@teranews>
I'm reading a text file, with a few non-lisp lines, followed by
several lines of an lisp object.  I get compiler warnings about unused
variables, which so far I've been able to ignore.  I've been recently
experimenting with CMUCL Wire protocol, and had used this text reading
function as the 'on-connect' function, but was surprised to see the
debugger pop up with the same complaint about unused vars.  So I
should fix this, but my original solution seems fine...

(defun reads-good-stuff ()
  (let ((in (open "filename" :if-does-not-exist nil)))
    (when in
      (let ((a (read-line in))))
      (let ((a (read-line in))))
      (let ((a (read-line in)))) ; discard 3 lines
      (let ((a (read in))	 ; the good stuff
	.....

Suggestions?
TIA

-- 
With sufficient thrust, pigs fly fine.

From: Eric Lavigne
Subject: Re: Skipping lines in text file
Date: 
Message-ID: <1118299843.889739.260970@o13g2000cwo.googlegroups.com>
>(defun reads-good-stuff ()
>  (let ((in (open "filename" :if-does-not-exist nil)))
>    (when in
>      (let ((a (read-line in))))
>      (let ((a (read-line in))))
>      (let ((a (read-line in)))) ; discard 3 lines
>      (let ((a (read in))        ; the good stuff
>        .....

>Suggestions?
>TIA

No need to create those extra variables if you won't be using them.

(defun reads-good-stuff ()
  (let ((in (open "filename" :if-does-not-exist nil)))
    (when in
      (read-line in)
      (read-line in)
      (read-line in) ; discard 3 lines
      (let ((a (read in))        ; the good stuff
        .....

You might even go a step further by using dotimes instead of repeating
the same line 3 times.

(defun reads-good-stuff ()
  (let ((in (open "filename" :if-does-not-exist nil)))
    (when in
      (dotimes (x 3) (read-line in)) ; discard 3 lines
      (let ((a (read in))        ; the good stuff
        .....
From: Kenny Tilton
Subject: Re: Skipping lines in text file
Date: 
Message-ID: <vVTpe.10598$XB2.2196345@twister.nyc.rr.com>
Eric Lavigne wrote:

>>(defun reads-good-stuff ()
>> (let ((in (open "filename" :if-does-not-exist nil)))
>>   (when in
>>     (let ((a (read-line in))))
>>     (let ((a (read-line in))))
>>     (let ((a (read-line in)))) ; discard 3 lines
>>     (let ((a (read in))        ; the good stuff
>>       .....
> 
> 
>>Suggestions?

Trivial one, but:

(loop repeat 3 do (read-line in))?

(dotimes (n 3) (readline in))?

-- 
Kenny

Why Lisp? http://lisp.tech.coop/RtL%20Highlight%20Film

"If you plan to enter text which our system might consider to be 
obscene, check here to certify that you are old enough to hear the 
resulting output." -- Bell Labs text-to-speech interactive Web page
From: GP lisper
Subject: Re: Skipping lines in text file
Date: 
Message-ID: <1118312102.13399137065a3d0aa992594d511c1ba8@teranews>
On Thu, 09 Jun 2005 09:24:43 GMT, <·······@nyc.rr.com> wrote:
> Eric Lavigne wrote:
>
>>>(defun reads-good-stuff ()
>>> (let ((in (open "filename" :if-does-not-exist nil)))
>>>   (when in
>>>     (let ((a (read-line in))))
>>>     (let ((a (read-line in))))
>>>     (let ((a (read-line in)))) ; discard 3 lines
>>>     (let ((a (read in))        ; the good stuff
>>>       .....
>
> Trivial one, but:
>
> (loop repeat 3 do (read-line in))?
>
> (dotimes (n 3) (readline in))?

Those are cleaner, I adopted the second one.  Thanks!


-- 
With sufficient thrust, pigs fly fine.
From: Nicolas Neuss
Subject: Re: Skipping lines in text file
Date: 
Message-ID: <87psuvzv7l.fsf@ortler.iwr.uni-heidelberg.de>
GP lisper <········@CloudDancer.com> writes:

> Those are cleaner, I adopted the second one.  Thanks!

I would choose the first one, because dotimes introduces a spurious
variable which is not used in the body which I dislike.  YMMV, of course.

Nicolas.
From: Pascal Bourguignon
Subject: Re: Skipping lines in text file
Date: 
Message-ID: <87oeaf2109.fsf@thalassa.informatimago.com>
Nicolas Neuss <·······@iwr.uni-heidelberg.de> writes:

> GP lisper <········@CloudDancer.com> writes:
>
>> Those are cleaner, I adopted the second one.  Thanks!
>
> I would choose the first one, because dotimes introduces a spurious
> variable which is not used in the body which I dislike.  YMMV, of course.

My millage would give:

(defmacro repeat (n &body body) 
  (let ((v (gensym))) `(do ((,v ,n (1- ,v))) ((<= ,v 0)) ,@body)))

and:

    (repeat 3 (read-line in))


Note that with dotimes you're doomed with most implementations.

Most implementations use v as loop variable, so it is actually used
and if you don't use it in the body and (declare (ignore v)) then
you'll get:

* (dotimes (v 3) (declare (ignore v)) (print :v))
;     (PSETQ V (1+ V))
; --> PSETF LET* MULTIPLE-VALUE-BIND LET PROGN 
; ==>
;   (SETQ V #:G0)
; 
; caught STYLE-WARNING:
;   V is being set even though it was declared to be ignored.

;     (1+ V)
; --> + 
; ==>
;   V
; 
; caught STYLE-WARNING:
;   reading an ignored variable: V

;     (>= V 3)
; --> IF < 
; ==>
;   V
; 
; caught STYLE-WARNING:
;   reading an ignored variable: V
; 
; compilation unit finished
;   caught 3 STYLE-WARNING conditions

:V 
:V 
:V 
NIL


While on implementations that use a different loop variable and only
bind v to it, if you omit the ignore:

 * (dotimes (v 3) (print :v))
;     (LET ((V #:G4425))
;     (PRINT :V))
; 
; caught STYLE-WARNING:
;   The variable V is defined but never used.
; 
; compilation unit finished
;   caught 1 STYLE-WARNING condition

:V 
:V 
:V 
NIL

So you cannot write portably a dotimes that doesn't use the variable.

I would say that the former implementations are deficient, but who I am?

-- 
__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: Lars Brinkhoff
Subject: Re: Skipping lines in text file
Date: 
Message-ID: <85fyvr91by.fsf@junk.nocrew.org>
Pascal Bourguignon <···@informatimago.com> writes:
> So you cannot write portably a dotimes that doesn't use the variable.

  (declare (ignorable v))
From: Raymond Wiker
Subject: Re: Skipping lines in text file
Date: 
Message-ID: <864qc74sib.fsf@raw.grenland.fast.no>
Pascal Bourguignon <···@informatimago.com> writes:

> Nicolas Neuss <·······@iwr.uni-heidelberg.de> writes:
>
>> GP lisper <········@CloudDancer.com> writes:
>>
>>> Those are cleaner, I adopted the second one.  Thanks!
>>
>> I would choose the first one, because dotimes introduces a spurious
>> variable which is not used in the body which I dislike.  YMMV, of course.
>
> My millage would give:
>
> (defmacro repeat (n &body body) 
>   (let ((v (gensym))) `(do ((,v ,n (1- ,v))) ((<= ,v 0)) ,@body)))
>
> and:
>
>     (repeat 3 (read-line in))
>
>
> Note that with dotimes you're doomed with most implementations.
>
> Most implementations use v as loop variable, so it is actually used
> and if you don't use it in the body and (declare (ignore v)) then
> you'll get:

        Could you use 

        (declare (ignoreable v))

instead?

-- 
Raymond Wiker                        Mail:  ·············@fast.no
Senior Software Engineer             Web:   http://www.fast.no/
Fast Search & Transfer ASA           Phone: +47 23 01 11 60
P.O. Box 1677 Vika                   Fax:   +47 35 54 87 99
NO-0120 Oslo, NORWAY                 Mob:   +47 48 01 11 60
From: GP lisper
Subject: Re: Skipping lines in text file
Date: 
Message-ID: <1118353506.3d2549e4dc2416fd06011538a75204b1@teranews>
On Thu, 09 Jun 2005 14:00:38 +0200, <···@informatimago.com> wrote:
> Nicolas Neuss <·······@iwr.uni-heidelberg.de> writes:
>> GP lisper <········@CloudDancer.com> writes:
>>
>>  ....somelines with LOOP vs DOTIMES
>>
>>> Those are cleaner, I adopted the second one.  Thanks!
>>
>> I would choose the first one, because dotimes introduces a spurious
>> variable which is not used in the body which I dislike.  YMMV, of course.
>
> My milage would give:
>
> (defmacro repeat (n &body body) 
>   (let ((v (gensym))) `(do ((,v ,n (1- ,v))) ((<= ,v 0)) ,@body)))
>
> and:
>
>     (repeat 3 (read-line in))

Well, after Nicolas comment, I expanded the dotimes macro in Slime and
thought that was a lot of lines for something I choose simply because
it was a readable English syntax.  I went to sleep thinking that a
'repeat macro' would be shorter and maybe I would write a first macro
later...

But I also thought about lisp needing to carry around the "v" counter,
when it is somewhat unnecessary (you know that only N simple lines are
required).  Maybe this is some compiler optimization, or maybe it's
some macro feature already [I haven't looked at macros, I like to
crawl for awhile first before I run in a language], but isn't it
possible to write something that simply emits the N lines of
(read-line in) without the counter?  There is some gcc term for this,
unwind-loops or something.


> Note that with dotimes you're doomed with most implementations.
>
> Most implementations use v as loop variable, so it is actually used
> and if you don't use it in the body and (declare (ignore v)) then
> you'll get:
>
> * (dotimes (v 3) (declare (ignore v)) (print :v))
> ;     (PSETQ V (1+ V))
> ; --> PSETF LET* MULTIPLE-VALUE-BIND LET PROGN 
> ; ==>
> ;   (SETQ V #:G0)
> ; 
> ; caught STYLE-WARNING:
> ;   V is being set even though it was declared to be ignored.


Hmm, CMUCL 19a from April 2004 didn't say a word...which was the
original goal.


-- 
With sufficient thrust, pigs fly fine.
From: Pascal Bourguignon
Subject: Re: Skipping lines in text file
Date: 
Message-ID: <87y89jywqb.fsf@thalassa.informatimago.com>
GP lisper <········@CloudDancer.com> writes:

> On Thu, 09 Jun 2005 14:00:38 +0200, <···@informatimago.com> wrote:
>> Nicolas Neuss <·······@iwr.uni-heidelberg.de> writes:
>>> GP lisper <········@CloudDancer.com> writes:
>>>
>>>  ....somelines with LOOP vs DOTIMES
>>>
>>>> Those are cleaner, I adopted the second one.  Thanks!
>>>
>>> I would choose the first one, because dotimes introduces a spurious
>>> variable which is not used in the body which I dislike.  YMMV, of course.
>>
>> My milage would give:
>>
>> (defmacro repeat (n &body body) 
>>   (let ((v (gensym))) `(do ((,v ,n (1- ,v))) ((<= ,v 0)) ,@body)))
>>
>> and:
>>
>>     (repeat 3 (read-line in))
>
> Well, after Nicolas comment, I expanded the dotimes macro in Slime and
> thought that was a lot of lines for something I choose simply because
> it was a readable English syntax.  I went to sleep thinking that a
> 'repeat macro' would be shorter and maybe I would write a first macro
> later...

You should rather check the generated code. 

[56]> (macroexpand '(dotimes (v 3) (read-line in)))
(BLOCK NIL
 (LET ((V 0))
  (TAGBODY #:G3691 (IF (>= V 3) (GO #:G3692)) (READ-LINE IN) (PSETQ V (1+ V))
   (GO #:G3691) #:G3692 (RETURN-FROM NIL (PROGN NIL))))) ;
T
[57]> (macroexpand '(repeat 3 (read-line in)))
(BLOCK NIL
 (LET ((#:G3693 3))
  (TAGBODY #:G3694 (IF (<= #:G3693 0) (GO #:G3695)) (READ-LINE IN)
   (PSETQ #:G3693 (1- #:G3693)) (GO #:G3694) #:G3695
   (RETURN-FROM NIL (PROGN))))) ;
T
[58]> (macroexpand '(loop repeat 3 do (read-line in)))
(MACROLET ((LOOP-FINISH NIL (SYSTEM::LOOP-FINISH-ERROR)))
 (BLOCK NIL
  (LET ((#:G3696 3))
   (PROGN
    (LET NIL
     (MACROLET ((LOOP-FINISH NIL '(GO SYSTEM::END-LOOP)))
      (TAGBODY SYSTEM::BEGIN-LOOP (UNLESS (PLUSP #:G3696) (LOOP-FINISH))
       (PROGN (PROGN (READ-LINE IN))) (SETQ #:G3696 (1- #:G3696))
       (GO SYSTEM::BEGIN-LOOP) SYSTEM::END-LOOP
       (MACROLET
        ((LOOP-FINISH NIL (SYSTEM::LOOP-FINISH-WARN)
          '(GO SYSTEM::END-LOOP))))))))))) ;
T

But if you look at the compiler output, 

dotimes takes 13 instructions,
repeat  (do) takes 12 instructions, and
loop    takes 12 instructions.

That is: they are all equivalent, with the small advantage of counting
by decrement allowed by repeat and loop repeat instead of dotimes.


[63]> (disassemble (compile nil '(lambda () (dotimes (v 3) (read-line in)))))
WARNING :
IN is neither declared nor bound,
it will be treated as if it were declared SPECIAL.

Disassembly of function NIL
(CONST 0) = 0
(CONST 1) = 3
(CONST 2) = IN
0 required arguments
0 optional arguments
No rest parameter
No keyword parameters
reads special variable: IN
13 byte-code instructions:
0     (CONST&PUSH 0)                      ; 0
1     (JMP L11)
3     L3
3     (GETVALUE&PUSH 2)                   ; IN
5     (PUSH-UNBOUND 3)
7     (CALLS1 121)                        ; READ-LINE
9     (LOAD&INC&STORE 0)
11    L11
11    (LOAD&PUSH 0)
12    (CONST&PUSH 1)                      ; 3
13    (CALLSR&JMPIFNOT 1 50 L3)           ; >=
17    (NIL)
18    (SKIP&RET 2)
NIL
[64]> 

[62]> (disassemble (compile nil '(lambda () (repeat 3 (read-line in)))))
WARNING :
IN is neither declared nor bound,
it will be treated as if it were declared SPECIAL.

Disassembly of function NIL
(CONST 0) = 3
(CONST 1) = IN
0 required arguments
0 optional arguments
No rest parameter
No keyword parameters
reads special variable: IN
12 byte-code instructions:
0     (CONST&PUSH 0)                      ; 3
1     (JMP L11)
3     L3
3     (GETVALUE&PUSH 1)                   ; IN
5     (PUSH-UNBOUND 3)
7     (CALLS1 121)                        ; READ-LINE
9     (LOAD&DEC&STORE 0)
11    L11
11    (LOAD&PUSH 0)
12    (CALLS2&JMPIF 147 L3)               ; PLUSP
15    (NIL)
16    (SKIP&RET 2)
NIL

[61]> (disassemble (compile nil '(lambda () (loop repeat 3 do (read-line in)))))
WARNING :
IN is neither declared nor bound,
it will be treated as if it were declared SPECIAL.

Disassembly of function NIL
(CONST 0) = 3
(CONST 1) = IN
0 required arguments
0 optional arguments
No rest parameter
No keyword parameters
reads special variable: IN
12 byte-code instructions:
0     (CONST&PUSH 0)                      ; 3
1     (JMP L11)
3     L3
3     (GETVALUE&PUSH 1)                   ; IN
5     (PUSH-UNBOUND 3)
7     (CALLS1 121)                        ; READ-LINE
9     (LOAD&DEC&STORE 0)
11    L11
11    (LOAD&PUSH 0)
12    (CALLS2&JMPIF 147 L3)               ; PLUSP
15    (NIL)
16    (SKIP&RET 2)
NIL



> But I also thought about lisp needing to carry around the "v" counter,
> when it is somewhat unnecessary (you know that only N simple lines are
> required).

To loop, you need a counter.  The bad thing with dotimes is that it
specifies that the variable increments from 0 to n-1, instead of
decrementing, which wins 1 machine instruction.


> Maybe this is some compiler optimization, or maybe it's
> some macro feature already [I haven't looked at macros, I like to
> crawl for awhile first before I run in a language], but isn't it
> possible to write something that simply emits the N lines of
> (read-line in) without the counter?  There is some gcc term for this,
> unwind-loops or something.

Of course this is something that you can implement, if you need it.
Note that when I/O is involved, CPU time or a counter-memory is not
worth the work.  But if you're looping in matrices or vectors, you can
easily do it.

(defmacro repeat (n &body body)
  (if (numberp n)
     `(progn ,@(loop repeat n collect `(progn ,@body)))
     (let ((v (gensym))) `(do ((,v ,n (1- ,v))) ((<= ,v 0)) ,@body))))


[65]> (macroexpand '(repeat 3 (read-line in)))
(PROGN (PROGN (READ-LINE IN)) (PROGN (READ-LINE IN)) (PROGN (READ-LINE IN))) ;
T
[66]> (macroexpand '(repeat (* 3 n) (read-line in)))
(BLOCK NIL
 (LET ((#:G3754 (* 3 N)))
  (TAGBODY #:G3755 (IF (<= #:G3754 0) (GO #:G3756)) (READ-LINE IN)
   (PSETQ #:G3754 (1- #:G3754)) (GO #:G3755) #:G3756
   (RETURN-FROM NIL (PROGN))))) ;


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
Litter box not here.
You must have moved it again.
I'll poop in the sink. 
From: Nicolas Neuss
Subject: Re: Skipping lines in text file
Date: 
Message-ID: <87psuuhbb2.fsf@ortler.iwr.uni-heidelberg.de>
GP lisper <········@CloudDancer.com> writes:

> about microoptimizations

Good advices:
1. Start optimizing only when you need it.
2. Optimize at other places.  Those micro-optimizations do
   usually not help at all in performance.

Nicolas.
From: Frode Vatvedt Fjeld
Subject: Re: Skipping lines in text file
Date: 
Message-ID: <2his0nr8tj.fsf@vserver.cs.uit.no>
Pascal Bourguignon <···@informatimago.com> writes:

> My millage would give:
>
> (defmacro repeat (n &body body) 
>   (let ((v (gensym))) `(do ((,v ,n (1- ,v))) ((<= ,v 0)) ,@body)))
>
> and:
>
> (repeat 3 (read-line in))

It seems pointless to me to reimplement the already standard loop
construct (loop repeat 3 do (read-line in)).

> So you cannot write portably a dotimes that doesn't use the
> variable.

That the compiler emits a style-warning (or most any warning) doesn't
realy mean your program isn't portable and conforming, does it?

-- 
Frode Vatvedt Fjeld
From: Pascal Bourguignon
Subject: Re: Skipping lines in text file
Date: 
Message-ID: <87k6l31vca.fsf@thalassa.informatimago.com>
Frode Vatvedt Fjeld <······@cs.uit.no> writes:
>> (repeat 3 (read-line in))
>
> It seems pointless to me to reimplement the already standard loop
> construct (loop repeat 3 do (read-line in)).

This is for those who prefer prefix syntax to infix syntax of loop.


>> So you cannot write portably a dotimes that doesn't use the
>> variable.
>
> That the compiler emits a style-warning (or most any warning) doesn't
> realy mean your program isn't portable and conforming, does it?

I'm not sure, I just think it's a matter of the specification being to
vague.  Perhaps it means the specified wanted us to use ignorable
instead of ignore in dotimes, but it has not the same semantics.  If I
want to make sure I don't use inadvertantly the variable in my body, I
want to ignore it. 


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

Nobody can fix the economy.  Nobody can be trusted with their finger
on the button.  Nobody's perfect.  VOTE FOR NOBODY.
From: Frode Vatvedt Fjeld
Subject: Re: Skipping lines in text file
Date: 
Message-ID: <2hekbbr1fc.fsf@vserver.cs.uit.no>
Frode Vatvedt Fjeld <······@cs.uit.no> writes:

>> It seems pointless to me to reimplement the already standard loop
>> construct (loop repeat 3 do (read-line in)).

Pascal Bourguignon <···@informatimago.com> writes:

> This is for those who prefer prefix syntax to infix syntax of loop.

I don't understand how

  (repeat 3 (read-line in))

is any more prefix (whatever that means here) than

  (loop repeat 3 do (read-line in))

-- 
Frode Vatvedt Fjeld
From: Pascal Bourguignon
Subject: Re: Skipping lines in text file
Date: 
Message-ID: <873brr17lb.fsf@thalassa.informatimago.com>
Frode Vatvedt Fjeld <······@cs.uit.no> writes:

> Frode Vatvedt Fjeld <······@cs.uit.no> writes:
>
>>> It seems pointless to me to reimplement the already standard loop
>>> construct (loop repeat 3 do (read-line in)).
>
> Pascal Bourguignon <···@informatimago.com> writes:
>
>> This is for those who prefer prefix syntax to infix syntax of loop.
>
> I don't understand how
>
>   (repeat 3 (read-line in))
>
> is any more prefix (whatever that means here) than
>
>   (loop repeat 3 do (read-line in))

Do you use: (+ 3 4 5)
or:         (please tell me what is 3 plus 4 plus 5)
or:         (compute 3 + 4 + 5)
?

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
Litter box not here.
You must have moved it again.
I'll poop in the sink. 
From: Ulrich Hobelmann
Subject: Re: Skipping lines in text file
Date: 
Message-ID: <3gst9fFe3a7pU3@individual.net>
Pascal Bourguignon wrote:
> Frode Vatvedt Fjeld <······@cs.uit.no> writes:
> 
> 
>>Frode Vatvedt Fjeld <······@cs.uit.no> writes:
>>
>>
>>>>It seems pointless to me to reimplement the already standard loop
>>>>construct (loop repeat 3 do (read-line in)).
>>
>>Pascal Bourguignon <···@informatimago.com> writes:
>>
>>
>>>This is for those who prefer prefix syntax to infix syntax of loop.
>>
>>I don't understand how
>>
>>  (repeat 3 (read-line in))
>>
>>is any more prefix (whatever that means here) than
>>
>>  (loop repeat 3 do (read-line in))
> 
> 
> Do you use: (+ 3 4 5)
> or:         (please tell me what is 3 plus 4 plus 5)
> or:         (compute 3 + 4 + 5)
> ?
> 

Assuming you have to add the + as a macro yourself, it might be 
easier to (apply #'+ 3 '(4 5)) (apply ~= compute).  The loop 
doesn't look THAT bad. ;)

-- 
Don't let school interfere with your education. -- Mark Twain
From: Thomas F. Burdick
Subject: Re: Skipping lines in text file
Date: 
Message-ID: <xcvd5qu4ps1.fsf@conquest.OCF.Berkeley.EDU>
Pascal Bourguignon <···@informatimago.com> writes:

> Frode Vatvedt Fjeld <······@cs.uit.no> writes:
> >> (repeat 3 (read-line in))
> >
> > It seems pointless to me to reimplement the already standard loop
> > construct (loop repeat 3 do (read-line in)).
> 
> This is for those who prefer prefix syntax to infix syntax of loop.

(loop :repeat 3 :do (read-line in)), then.  In this case, LOOP reads
just like a function with keyword arguments.

-- 
           /|_     .-----------------------.                        
         ,'  .\  / | Free Mumia Abu-Jamal! |
     ,--'    _,'   | Abolish the racist    |
    /       /      | death penalty!        |
   (   -.  |       `-----------------------'
   |     ) |                               
  (`-.  '--.)                              
   `. )----'                               
From: Pascal Bourguignon
Subject: Re: Skipping lines in text file
Date: 
Message-ID: <873brqyvl0.fsf@thalassa.informatimago.com>
···@conquest.OCF.Berkeley.EDU (Thomas F. Burdick) writes:

> Pascal Bourguignon <···@informatimago.com> writes:
>
>> Frode Vatvedt Fjeld <······@cs.uit.no> writes:
>> >> (repeat 3 (read-line in))
>> >
>> > It seems pointless to me to reimplement the already standard loop
>> > construct (loop repeat 3 do (read-line in)).
>> 
>> This is for those who prefer prefix syntax to infix syntax of loop.
>
> (loop :repeat 3 :do (read-line in)), then.  In this case, LOOP reads
> just like a function with keyword arguments.

Good point  :-)

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
You're always typing.
Well, let's see you ignore my
sitting on your hands.
From: Nicolas Neuss
Subject: Re: Skipping lines in text file
Date: 
Message-ID: <87zmtz4ore.fsf@ortler.iwr.uni-heidelberg.de>
Pascal Bourguignon <···@informatimago.com> writes:

> My millage would give:
>
> (defmacro repeat (n &body body) 
>   (let ((v (gensym))) `(do ((,v ,n (1- ,v))) ((<= ,v 0)) ,@body)))

Does your mileage also provide us with the suitable patches for all the
editors around such that

(repeat 3
  something)

is properly indented?

Nicolas.
From: Frank Buss
Subject: Re: Skipping lines in text file
Date: 
Message-ID: <rtd5wgaeqmiz$.1jc10hsaysf8n$.dlg@40tude.net>
Nicolas Neuss wrote:

> Does your mileage also provide us with the suitable patches for all the
> editors around such that
> 
> (repeat 3
>   something)
> 
> is properly indented?

perhaps you should just use the right editor. LispWorks indents it
properly, after compiling the macro.

-- 
Frank Bu�, ··@frank-buss.de
http://www.frank-buss.de, http://www.it4-systems.de
From: Nicolas Neuss
Subject: Re: Skipping lines in text file
Date: 
Message-ID: <878y1jobxm.fsf@ortler.iwr.uni-heidelberg.de>
Frank Buss <··@frank-buss.de> writes:

> perhaps you should just use the right editor. LispWorks indents it
> properly, after compiling the macro.

OK, you're right.  SLIME works, too.  Thus, this is probably not a problem
in reality.
Nicolas.
From: Marco Baringer
Subject: Re: Skipping lines in text file
Date: 
Message-ID: <m2k6l3r55h.fsf@soma.local>
Nicolas Neuss <·······@iwr.uni-heidelberg.de> writes:

> Pascal Bourguignon <···@informatimago.com> writes:
>
>> My millage would give:
>>
>> (defmacro repeat (n &body body) 
>>   (let ((v (gensym))) `(do ((,v ,n (1- ,v))) ((<= ,v 0)) ,@body)))
>
> Does your mileage also provide us with the suitable patches for all the
> editors around such that
>
> (repeat 3
>   something)
>
> is properly indented?

as long as you're using allegro, lispworks, fred (mcl) or slime no
patches are neccessary.

-- 
-Marco
Ring the bells that still can ring.
Forget the perfect offering.
There is a crack in everything.
That's how the light gets in.
	-Leonard Cohen
From: Ulrich Hobelmann
Subject: Re: Skipping lines in text file
Date: 
Message-ID: <3gr6s3Fdu9lnU1@individual.net>
Nicolas Neuss wrote:
> Pascal Bourguignon <···@informatimago.com> writes:
> 
> 
>>My millage would give:
>>
>>(defmacro repeat (n &body body) 
>>  (let ((v (gensym))) `(do ((,v ,n (1- ,v))) ((<= ,v 0)) ,@body)))
> 
> 
> Does your mileage also provide us with the suitable patches for all the
> editors around such that
> 
> (repeat 3
>   something)
> 
> is properly indented?

I think that's what the &body (in contrast to &rest) is for...
(assuming all decent editors know and respect that)

-- 
Don't let school interfere with your education. -- Mark Twain
From: Pascal Costanza
Subject: Re: Skipping lines in text file
Date: 
Message-ID: <3gr1plFdmn55U1@individual.net>
Pascal Bourguignon wrote:

> My millage would give:
> 
> (defmacro repeat (n &body body) 
>   (let ((v (gensym))) `(do ((,v ,n (1- ,v))) ((<= ,v 0)) ,@body)))
> 
> and:
> 
>     (repeat 3 (read-line in))
> 
> Note that with dotimes you're doomed with most implementations
[because of ambiguity across CL implementations wrt whether the counter 
variable is captured or not.]

What about this?

(defmacro repeat (count &body body)
   (let ((v (gensym)))
     `(dotimes (v ,count)
        (locally (declare (ignore ,v))
          ,@body))))


Pascal

-- 
2nd European Lisp and Scheme Workshop
July 26 - Glasgow, Scotland - co-located with ECOOP 2005
http://lisp-ecoop05.bknr.net/
From: Peter Lewerin
Subject: Re: Skipping lines in text file
Date: 
Message-ID: <b72f3640.0506090323.693468ad@posting.google.com>
GP lisper <········@CloudDancer.com> wrote

> (defun reads-good-stuff ()
>   (let ((in (open "filename" :if-does-not-exist nil)))
>     (when in
>       (let ((a (read-line in))))
>       (let ((a (read-line in))))
>       (let ((a (read-line in)))) ; discard 3 lines
>       (let ((a (read in))	 ; the good stuff
> 	.....

Eric showed you a better way to express this.  I'd just like to point
out that even if you were going to use the above method, there's no
need to create and destroy four lexical scopes in a row.  This is
somewhere halfway between your and Eric's solutions:

    (when in
      (let (a)
        (setf a (read-line in))
        (setf a (read-line in))
        (setf a (read-line in))
        (setf a (read in))
        ...))
From: Edi Weitz
Subject: Re: Skipping lines in text file
Date: 
Message-ID: <upsuvydaf.fsf@agharta.de>
On 9 Jun 2005 04:23:54 -0700, ·············@swipnet.se (Peter Lewerin) wrote:

>         (setf a (read-line in))
>         (setf a (read-line in))
>         (setf a (read-line in))
>         (setf a (read in))

You could even save some keystrokes and write it like this

          (setf a (read-line in)
                a (read-line in)
                a (read-line in)
                a (read in))

Cheers,
Edi.

-- 

Lisp is not dead, it just smells funny.

Real email: (replace (subseq ·········@agharta.de" 5) "edi")
From: GP lisper
Subject: Re: Skipping lines in text file
Date: 
Message-ID: <1118352605.38c63d6483c21b3c7a3f61c5e1f79288@teranews>
On 9 Jun 2005 04:23:54 -0700, <·············@swipnet.se> wrote:
>
> Eric showed you a better way to express this.  I'd just like to point
> out that even if you were going to use the above method, there's no
> need to create and destroy four lexical scopes in a row.  This is
> somewhere halfway between your and Eric's solutions:
>
>     (when in
>       (let (a)
>         (setf a (read-line in))
>         (setf a (read-line in))
>         (setf a (read-line in))
>         (setf a (read in))
>         ...))


Nice, I hadn't thought of the separate 'let'.  I'm a bit surprised
such a simple problem contains so many intriguing bits shown in the
thread.


-- 
With sufficient thrust, pigs fly fine.
From: Ulrich Hobelmann
Subject: Re: Skipping lines in text file
Date: 
Message-ID: <3gq92nFdm2tkU1@individual.net>
GP lisper wrote:
> I'm reading a text file, with a few non-lisp lines, followed by
> several lines of an lisp object.  I get compiler warnings about unused
> variables, which so far I've been able to ignore.  I've been recently
> experimenting with CMUCL Wire protocol, and had used this text reading
> function as the 'on-connect' function, but was surprised to see the
> debugger pop up with the same complaint about unused vars.  So I
> should fix this, but my original solution seems fine...
> 
> (defun reads-good-stuff ()
>   (let ((in (open "filename" :if-does-not-exist nil)))
>     (when in
>       (let ((a (read-line in))))
>       (let ((a (read-line in))))
>       (let ((a (read-line in)))) ; discard 3 lines
>       (let ((a (read in))	 ; the good stuff
> 	.....

How about
(defun reads-good-stuff (filename)
   (with-open-file (in filename)
     (read-line in)
     (read-line in)
     (read-line in)
     (read in)))
?
You could catch the file-not-found condition, maybe, somewhere.

-- 
Don't let school interfere with your education. -- Mark Twain
From: GP lisper
Subject: Re: Skipping lines in text file
Date: 
Message-ID: <1118304005.91d578b1e8c7f508000046d186f378e4@teranews>
On Thu, 09 Jun 2005 09:23:12 +0200, <···········@web.de> wrote:
>
>
> GP lisper wrote:
>> I'm reading a text file, with a few non-lisp lines, followed by
>> several lines of an lisp object.  I get compiler warnings about unused
>> variables, which so far I've been able to ignore.  I've been recently
>> experimenting with CMUCL Wire protocol, and had used this text reading
>> function as the 'on-connect' function, but was surprised to see the
>> debugger pop up with the same complaint about unused vars.  So I
>> should fix this, but my original solution seems fine...
>> 
>> (defun reads-good-stuff ()
>>   (let ((in (open "filename" :if-does-not-exist nil)))
>>     (when in
>>       (let ((a (read-line in))))
>>       (let ((a (read-line in))))
>>       (let ((a (read-line in)))) ; discard 3 lines
>>       (let ((a (read in))	 ; the good stuff
>> 	.....
>
> How about
> (defun reads-good-stuff (filename)
>    (with-open-file (in filename)
>      (read-line in)
>      (read-line in)
>      (read-line in)
>      (read in)))
> ?

Seems to be fine, thanks!


-- 
With sufficient thrust, pigs fly fine.
From: Espen Vestre
Subject: Re: Skipping lines in text file
Date: 
Message-ID: <kwy89jsw3k.fsf@merced.netfonds.no>
GP lisper <········@CloudDancer.com> writes:

> (defun reads-good-stuff ()
>   (let ((in (open "filename" :if-does-not-exist nil)))
>     (when in
>       (let ((a (read-line in))))
>       (let ((a (read-line in))))
>       (let ((a (read-line in)))) ; discard 3 lines
>       (let ((a (read in))	 ; the good stuff
> 	.....
>
> Suggestions?

Others have already suggested using e.g. dotimes, I'd just like
to add that you're probably(*) better off using with-open-file,
which closes the file for you after you leave its scope (implicit
unwind-protect). I.e.:

 (defun reads-good-stuff ()
   (with-open-file (in "filename" :if-does-not-exist nil)
     (when in
       (dotimes (i 3)(read-line in))
       (let ((a (read in))	 ; the good stuff
 	.....

(*) a possible exception exists if the open file is needed after the 
function returns.
-- 
  (espen)
From: Wade Humeniuk
Subject: Re: Skipping lines in text file
Date: 
Message-ID: <If9qe.57976$9A2.26706@edtnps89>
GP lisper wrote:
> I'm reading a text file, with a few non-lisp lines, followed by
> several lines of an lisp object.  I get compiler warnings about unused
> variables, which so far I've been able to ignore.  I've been recently
> experimenting with CMUCL Wire protocol, and had used this text reading
> function as the 'on-connect' function, but was surprised to see the
> debugger pop up with the same complaint about unused vars.  So I
> should fix this, but my original solution seems fine...
> 

If you have control over the format of the text file you might consider
changing the first three lines to Lisp Comments.  Then just use read.

Wade
From: Wade Humeniuk
Subject: Re: Skipping lines in text file
Date: 
Message-ID: <6p9qe.57977$9A2.16539@edtnps89>
GP lisper wrote:
> I'm reading a text file, with a few non-lisp lines, followed by
> several lines of an lisp object.  I get compiler warnings about unused
> variables, which so far I've been able to ignore.  I've been recently
> experimenting with CMUCL Wire protocol, and had used this text reading
> function as the 'on-connect' function, but was surprised to see the
> debugger pop up with the same complaint about unused vars.  So I
> should fix this, but my original solution seems fine...
> 

If you have control over the format of the text file you might consider
changing the first three lines to Lisp Comments.  Then just use read.

Wade
From: Wade Humeniuk
Subject: Re: Skipping lines in text file
Date: 
Message-ID: <Quaqe.44299$on1.33548@clgrps13>
GP lisper wrote:
> I'm reading a text file, with a few non-lisp lines, followed by
> several lines of an lisp object.  I get compiler warnings about unused
> variables, which so far I've been able to ignore.  I've been recently
> experimenting with CMUCL Wire protocol, and had used this text reading
> function as the 'on-connect' function, but was surprised to see the
> debugger pop up with the same complaint about unused vars.  So I
> should fix this, but my original solution seems fine...
> 


If you have control over the format of the text file you might consider
changing the first three lines to Lisp Comments.  Then just use read.

Wade