HOME |
8. Higher Order Functions |
Yet Another Scheme Tutorial |
10. Assignment |
Post Messages |
In this chapter, I will explain about input and output. Using this feature, you can read and write data from/to files.
[code 1]
(define (read-file file-name)
(let ((p (open-input-file file-name)))
(let loop((ls1 '()) (c (read-char p)))
(if (eof-object? c)
(begin
(close-input-port p)
(list->string (reverse ls1)))
(loop (cons c ls1) (read-char p))))))
For instance, the result shown in [example 1] is obtained by applying the [code 1] to a file [hello.txt].
As the newline character is represented by '\n', it is not easy to read.
Function display is available, however, for formatting ([example 2]).
[hello.txt]
Hello world! Scheme is an elegant programming language.[example 1]
(cd "C:\\doc")
(read-file "hello.txt")
;Value 14: "Hello world!\nScheme is an elegant programming language.\n"
[example 2]
(display (read-file "hello.txt"))
Hello world!
Scheme is an elegant programming language.
;Unspecified return value
[code 2]
(define (read-file file-name)
(call-with-input-file file-name
(lambda (p)
(let loop((ls1 '()) (c (read-char p)))
(if (eof-object? c)
(begin
(close-input-port p)
(list->string (reverse ls1)))
(loop (cons c ls1) (read-char p)))))))
[code 3]
(define (read-file file-name)
(with-input-from-file file-name
(lambda ()
(let loop((ls1 '()) (c (read-char)))
(if (eof-object? c)
(list->string (reverse ls1))
(loop (cons c ls1) (read-char)))))))
[paren.txt]
'(Hello world! Scheme is an elegant programming language.) '(Lisp is a programming language ready to evolve.)[code 4]
(define (s-read file-name)
(with-input-from-file file-name
(lambda ()
(let loop ((ls1 '()) (s (read)))
(if (eof-object? s)
(reverse ls1)
(loop (cons s ls1) (read)))))))
The following shows the result of reading paren.txt by s-read.
(s-read "paren.txt")
⇒ ((quote (hello world! scheme is an elegant programming language.))
(quote (lisp is a programming language ready to evolve.)))
(read-lines "hello.txt") ⇒ ("Hello world!" "Scheme is an elegant programming language.")
I will explain about assignment on Scheme in the next chapter.
(define (group-list ls sep)
(letrec ((iter (lambda (ls0 ls1)
(cond
((null? ls0) (list ls1))
((eqv? (car ls0) sep)
(cons ls1 (iter (cdr ls0) '())))
(else (iter (cdr ls0) (cons (car ls0) ls1)))))))
(map reverse (iter ls '()))))
(define (read-lines file-name)
(with-input-from-file file-name
(lambda ()
(let loop((ls1 '()) (c (read-char)))
(if (eof-object? c)
(map list->string (group-list (reverse ls1) #\Linefeed)) ; *
(loop (cons c ls1) (read-char)))))))
example:
(group-list '(1 4 0 3 7 2 0 9 5 0 0 1 2 3) 0) ;Value 13: ((1 4) (3 7 2) (9 5) () (1 2 3)) (read-lines "hello.txt") ;Value 14: ("Hello world!" "Scheme is an elegant programming language." "")
(define (my-copy-file from to)
(let ((pfr (open-input-file from))
(pto (open-output-file to)))
(let loop((c (read-char pfr)))
(if (eof-object? c)
(begin
(close-input-port pfr)
(close-output-port pto))
(begin
(write-char c pto)
(loop (read-char pfr)))))))
(define (print-lines . lines)
(let loop((ls0 lines))
(if (pair? ls0)
(begin
(display (car ls0))
(newline)
(loop (cdr ls0))))))
HOME |
8. Higher Order Functions |
Yet Another Scheme Tutorial |
10. Assignment |
Post Messages |