Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Newbie asking for help
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Thom Goodsell  
View profile  
 More options Jun 22 2000, 3:00 am
Newsgroups: comp.lang.lisp
From: Thom Goodsell <t...@cra.com>
Date: 2000/06/22
Subject: Re: Newbie asking for help

> (with-open-file (ifile "key.html" :direction :input)
>                 (setf nlines 0)
>                 (loop
>                   (if (read-line ifile nil nil)
>                     (setf nlines (+ 1 nlines))
>                     (return (print nlines)))))

> So, i plod along, undaunted...
> I'm pretty sure this is an ugly program, and i appreciate suggestions.

Ok, a few suggestions, then.  As Erik mentioned, nlines is a free
variable.  Use a let form.  It's probably stylistic, but I prefer the
much-more-compact (incf) function to using (set x (+ 1 x)).  Also, you
can replace the if construct with features of the loop facility -- use
while and finally.  Some people may argue this stylistically, but I like
prefer it [1].

So, the revised, slightly cleaner code looks like this:

(with-open-file (ifile "key.html" :direction :input)
  (let ((nlines 0))
    (loop
       while (read-line ifile nil nil)
       do (incf nlines)
       finally (return nlines))))

The even more compact version uses the for and from loop functionality:

(with-open-file (ifile "key.html" :direction :input)
  (loop
    for nlines from 0
    while (read-line ifile nil nil)
    finally (return nlines)))

Thom

[1] I learned my Lisp somewhat in a vacuum, so I sometimes find that
what I consider elegant or clean, other people consider ugly or unclear.

Thom Goodsell
Scientist                               t...@cra.com
Charles River Analytics         (617) 491-3474 x574
Cambridge, MA, USA              http://www.cra.com/


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.