The function (open-input-file filename) is available to open a file. This function return a port for input. The function (read-char port) is to read a character from the port. As this function returns eof-object when it reaches the end of the file (EOF), you can check it by using eof-object?. The function (close-input-port port) is to close the input port. The [code 1] shows a function that returns file contents as string.html
[code 1]express
(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))))))
爲何要reverse,由於cons c ls1 是把先前讀取的放在後面,而c放在前面,爲何不(cons ls1 c)
#\!)
> (define c (cons 1 2))
> (cons c 3)
'((1 . 2) . 3)
> (cons 3 c)
'(3 1 . 2)app
把cons放在car位置上,會有一個(),而放在第二個則沒有。函數
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]).oop
[hello.txt]post
Hello world! Scheme is an elegant programming language.
[example 1]ui
(cd "C:\\doc")
(read-file "hello.txt")
;Value 14: "Hello world!\nScheme is an elegant programming language.\n"
[example 2]this
(display (read-file "hello.txt"))
Hello world!
Scheme is an elegant programming language.
;Unspecified return value
You can open a file for input using the syntax call-with-input-file or with-input-from-file. These syntaxes are convenient because they handle errors.spa
它打開一個文件來做爲輸入,接受一個函數(port作爲參數)設計
[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)))))))
The function (read port) reads a S-expression from the port. It is convenient to read contents with parentheses like [paren.txt]. (這個真是爲lisp專門設計的。
[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.)))
demo1:
'(hello wrodl)
'(you )
'(can)
輸出:'('(hello wrodl) '(you) '(can))
demo2:
(you) (can)
'((you) (can))
demo3:
(you (we)) (can)
輸出:'((you (we)) (can))
Write the function read-lines that returns a list of strings which correspond to each line of file contents. The newline character is represented by#\Linefeed in Scheme. Following is the result of applying this function to the hello.txt.
(read-lines "hello.txt") ⇒ ("Hello world!" "Scheme is an elegant programming language.")
Similar functions to those for input are available to make output ports.
Following functions for output are available. These functions output to the standard output if the port is omitted.
1
2
3
|
scheme@(guile-user)
>
(display 9)
9scheme@(guile-user)
>
(display 9 (current-output-port))
9scheme@(guile-user)
>
|
scheme@(guile-user)> (define i (open-input-file "hello.txt")) scheme@(guile-user)> (read-char i) #\h scheme@(guile-user)> (define j (read i)) scheme@(guile-user)> j ello scheme@(guile-user)> (read-char i) #\newline scheme@(guile-user)> (read-char i) #<eof>
1
2
3
4
5
6
|
scheme@(guile-user)
>
(define o (open-output-file
"greeting.txt"
))
scheme@(guile-user)
>
(display
"hello"
o)
scheme@(guile-user)
>
(write-char #\space o)
scheme@(guile-user)
>
(display
'world
o)
scheme@(guile-user)
>
(newline o)
scheme@(guile-user)
>
(close-output-port o)
|
scheme@(guile-user)
>
(call-with-input-file
"hello.txt"
... (
lambda
(i)
... (
let
* ((a (read-char i))
... (b (read-char i))
... (c (read-char i)))
... (
list
a b c))))
(#\h #\e #\l)
|
scheme@(guile-user)
>
(define i (open-input-string
"hello world"
))
scheme@(guile-user)
>
(read-char i)
#\h
scheme@(guile-user)
>
(read i)
ello
scheme@(guile-user)
>
(read i)
world
scheme@(guile-user)
>
(read i)
#
<
eof
>
|
參考:
http://www.shido.info/lisp/scheme9_e.html
http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-9.html