【发布时间】:2015-05-29 22:12:17
【问题描述】:
我是 Scheme 的新手,这是家庭作业,所以我要求的是提示而不是完整的解决方案。我正在编写一个名为 type-checked 的过程,它将过程和零个或多个类型谓词作为参数。该过程的值是一个可变参数过程:如果它的参数与给定类型检查的相应类型匹配,则它返回在参数上调用的过程的值。否则,它会报告错误。
我有这个程序适用于这样的事情:
((type-checked sqrt number?) 100)
但不是为了这个:
((type-checked + number?) 1 2 3 4 5)
也就是说,我可以使用一个参数正确运行该过程,但不能使用可变数量的参数。以下是相关代码:
(define (type-checked procedure types)
(lambda args
(if (types-match? (list types) (list args))
(procedure args)
(error "type mismatch"))))
如果我用括号括起来 args,我可以在一个参数上运行它。否则,我总是会收到类型不匹配错误。
这是类型检查调用的递归过程。它检查给定的类型是否与参数匹配。我知道它没有优化,但现在我的目标是工作代码。类型匹配?接受两个列表,类型谓词和值,并检查它们是否都匹配。我试图以一种有意义的方式对其进行评论。下面的代码似乎可以独立运行。
(define types-match?
(lambda (types values)
(if (= 1 (length types)) ;if there is only one type left
(if (null? values) ;if values is empty, finished
#t
(if ((car types) (car values)) ;else, check types with the rest of the list
(types-match? types (cdr values))
#f))
(if (null? values)
#t
(if ((car types) (car values)) ;if there is more than one type in types, call
(types-match? (cdr types) (cdr values)) ;compare with the first type in the list, then call on the rest of both types and values
#f)))))
我试图弄清楚在调用过程时如何接受可变数量的参数。非常感谢任何帮助,并提前感谢您!
【问题讨论】: