【发布时间】:2013-04-08 20:27:01
【问题描述】:
首先我会提出问题。
假设将输入定义为一个返回“istream”的函数 - 一个承诺,当强制执行时将产生一对,其 cdr 是 istream:
(define input (lambda () (delay (cons (read) (input)))))
现在我们可以将驱动程序定义为期望一个“ostream”——一个空列表或一对,其中的 cdr 是一个 ostream:
(define driver
(lambda (s)
(if (null? s) '()
(begin
(display (car s))
(driver (force (cdr s)))))))
注意使用武力。
展示如何编写函数 squares,使其接受 istream 作为参数并返回 ostream。然后您应该能够输入 (driver (squares (input))) 并查看相应的行为。
书上的方块在上面。
(define squares (lambda (a)
(cons "please enter a number\n"
(let ((n (car a)))
(if (eof-object? n) '()
(cons (* n n) (cons #\newline (squares (cdr a)))))))))
(define output (squares (input)))
我不知道如何解决它以及我可以从哪里开始。请帮忙。
【问题讨论】:
标签: scheme