我是Scheme的新手,这是家庭作业,所以我要求提示而不是整个解决方案。我正在编写一个名为type-checked的过程,它将过程和零个或多个类型谓词作为参数。该过程的值是一个变量arity过程:如果它的参数匹配给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)))))
我试图弄清楚在调用过程时如何接受可变数量的参数。非常感谢任何帮助,并提前感谢您!
答案 0 :(得分:2)
types
需要像args一样成为休息参数,因为两个参数都是您不需要将它们包装在列表中的值列表,您需要使用apply
来使用列表作为过程的多个参数:
(define (type-checked procedure . types) ; types is a rest argument
(lambda args ; args is a rest argument
(if (types-match? types args) ; both are lists
(apply procedure args) ; (apply + '(1 2)) == (+ 1 2)
(error "type mismatch"))))
我发现当你进行多类型参数列表的最后一次迭代时,它会假设所有最后一个参数都是你提供的最后一个类型。例如。以下内容实际上有效:
((types-match? map procedure? list?) + '(1 2 3) '(1 2 3)) ; ==> (2 4 6)