s c h e m e w i k i . o r g

/ WebHome / Sandbox.TestTopic1

This Web


WebHome 
WebChanges 
WebTopicList 
WebStatistics 

All Webs


Chicken
Cookbook
Erlang
Know
Main
Plugins
Sandbox
Scm
TWiki  

Schematics


Schematics Home
TWiki Shorthand

Offsite Links


Schemers.org
Lambda the Ultimate
twiki.org
Here's some source code formatted by ScmToXexpr (ignore spurious %begin% markup, which comes from the source files being used):

%begin scheme%
(define (for-each-line-in-file filename proc . mode)
  (with-input-from-file
   filename
   (lambda () (apply for-each-line proc (current-input-port) mode))))

(define (for-each-line proc . port+mode)
  (let while ()
    (let ((line (apply read-line port+mode)))
      (unless (eof-object? line)
        (proc line)
        (while)))))

%end%

Here's the same using the BeautifierPlugin :

%begin scheme%
(define (for-each-line-in-file filename proc . mode)
  (with-input-from-file
    filename
    (lambda () (apply for-each-line proc (current-input-port) mode))))

(define (for-each-line proc . port+mode)
  (let while ()
    (let ((line (apply read-line port+mode)))
      (unless (eof-object? line)
        (proc line)
        (while)))))
%end%

Here's an example of the above in action:

%begin scheme%
(for-each-line-in-file
  "ReadingLines.scm"
  (let ((line-num 0))
    (lambda (line) 
      (printf "~a: ~a ~n" line-num line)
      (set! line-num (add1 line-num)))))

%end%

Here's the functional version:

%begin scheme%
(define (fold-lines-in-file filename proc init . mode)
  (with-input-from-file
   filename
   (lambda () (apply fold-lines proc init (current-input-port) mode))))

(define (fold-lines proc init . port+mode)
  (let while ((accum init))
    (let ((line (apply read-line port+mode)))
      (if (eof-object? line) accum
          (while (proc line accum))))))

%end%

Here's an example of the functional version in action:

%begin scheme%
(fold-lines-in-file
  "ReadingLinesWithFold.scm"
  (lambda (line line-num) 
    (printf "~a: ~a ~n" line-num line)
    (add1 line-num)) 
  1)

%end%

-- AntonVanStraaten - 04 Apr 2004

 
 
© 2004 by the contributing authors. / You are Main.guest