Name: Anonymous 2012-07-12 8:38
I'm fairly new to Scheme and I wrote the following code:
I'm just wondering whether there's a better way to handle eof that doesn't require the use of a separate variable. Also, do you know of a more portable way for writing to-digit? Thanks!
(define (to-digit c)
(- (char->integer c) (char->integer #\0)))
(define eof-flag #f)
(define (proc c)
(cond ((eof-object? c) (set! eof-flag #t) 0)
((char=? c #\newline) 0)
((char-numeric? c) (+ (to-digit c) (proc (read-char))))
(else (proc (read-char)))))
(define (main)
(display (proc (read-char)))
(newline)
(cond (eof-flag #t)
(else (main))))
(main)I'm just wondering whether there's a better way to handle eof that doesn't require the use of a separate variable. Also, do you know of a more portable way for writing to-digit? Thanks!