首页

2009年5月14日星期四

Scheme/Lisp高级属性(1)


------------------------------------------
;;;;do相当于c语言里的for.
(do ((i 0 (+ i 1))
(j 0 (- j 1)))
((= (+ i j) 2))
(begin
(display "laohuo")
(newline)
(display "henlaohuo")
(newline)))
-------------------------------------------
;;;;判断读取一个文件的某一字符,当程序读取时判断当前字符是否x如果是 则终止程序打印出该字符的前一部分 no.直到读取到这一个字符才停止。
(let ((in (open-input-file "foo.bar")))
(do ((c (read-char in) (read-char in)))
((eqv? c #\x)
(degin
'yes
(close-input-port in))
(degin
(display c))) c))
-------------------------------------------
;;;;判断读取一个文件的某一段,当程序读取到字符#或g打印出yes,如果读取到m或n 则打印 no.直到读取到这两个判断中的某一个字符才停止
(let ((in (open-input-file "foo.bar")))
(let loop ((c (read-char in)))
((case c
((#\# #\g) 'yes)
((#\m #n) 'no)
(else
(loop (read-char in))))))
-------------------------------------------
read-line (读取整行)

没有评论:

发表评论