scsh-users
[Top] [All Lists]

Re: Code critique requested

To: scheming_bugger@yahoo.com (Scheming Bugger)
Subject: Re: Code critique requested
From: sperber@informatik.uni-tuebingen.de (Michael Sperber [Mr. Preprocessor])
Date: Fri, 09 Aug 2002 17:57:23 +0200
Cc: scsh-news@zurich.ai.mit.edu
>>>>> "SB" == Scheming Bugger <scheming_bugger@yahoo.com> writes:

SB> ;; Return the number of files in the tree rooted at the specified
SB> ;; base directory that have been modified since the specified date.
SB> ;; Modified directories are not counted.
SB> (define (count-recent-files base-dir since-date)
SB>   (if (and (file-exists? base-dir)
SB>        (file-directory? base-dir))
SB>       (with-cwd base-dir
SB>             (let ((count 0))
SB>               (for-each (lambda (file)
SB>                           (if (file-directory? file)
SB>                               (set! count
SB>                                     (+ count
SB>                                        (count-recent-files
SB>                                         file
SB>                                         since-date)))
SB>                               (if (< since-date (file-last-mod file))
SB>                                   (set! count (+ 1 count)))))
SB>                         (directory-files))
SB>               count))
SB>       #f))

SB>      My primary question here, in addition to any style issues, is
SB> whether or not I should be concerned about the use of set!.  Is set!
SB> an indication that I should be looking harder for a solution that
SB> doesn't have side-effects?

Probably not in this case: that part of the code looks fine.

SB>      A secondary question:  Does it make sense to return #f if the
SB> specified base-dir doesn't exist?

It does, but it would be simpler to just omit the top-level IF and
rely on exception handling to notify you when things go wrong.  The
same holds for the example below:

SB> ;; Return the date of the last login of the specified user.
SB> (define (last-login name)
SB>   (let ((read-user-file (field-reader (infix-splitter)))
SB>     (last-login-time "0"))
SB>     (if (file-exists? (user-file-name name))
SB>     (call-with-input-file (user-file-name name)
SB>       (lambda (port)
SB>         (awk (read-user-file port) (record fields) ()
SB>              ((equal? (car fields) *last-login-field-name*)
SB>               (set! last-login-time (cadr fields)))))))
SB>     (string->number last-login-time)))

SB>      Here again I found it necessary to use set!.  Is there a better
SB> way to use awk?  Should I do something different to avoid the
SB> string->number conversion?

Yes; avoid AWK.  Here's a quick shot at what I'd write:

(define (last-login name)
  (let* ((lines (port->string-list (open-input-file (user-file-name name))))
         (last-fields
          (find (lambda (fields)
                  (equal? (car fields) *last-login-field-name*))
                (map (infix-splitter)
                     (reverse lines)))))
    (string->number (cadr last-fields))))

-- 
Cheers =8-} Mike
Friede, Völkerverständigung und überhaupt blabla

<Prev in Thread] Current Thread [Next in Thread>