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.
>(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
.....
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
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.
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
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
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.
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.
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.
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
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.
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
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.
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
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! |
( -. | `-----------------------'
| ) |
(`-. '--.)
`. )----'
···@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.
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.
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
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.
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
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
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))
...))
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")
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.
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
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.
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)
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
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
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