我正在阅读一本关于编程语言的教科书,其中一个练习是在 Scheme 中创建一个函数来翻转列表中的元组。这是我的代码:
; invert : Listof(List(Int,Int)) -> Listof(List(Int,int))
; usage: (invert '((a 1) (a 2) (1 b) (2 b))) -> ((1 a) (2 a) (b 1) (b 2))
(define invert
(lambda (lst)
(if (null? lst)
'()
(cons
(flip (car lst))
(invert (cdr lst))))))
; flip : List(Int,Int) -> List(Int,int)
; usage: (flip '(a 1)) -> (1 a)
(define flip
(lambda (tuple)
(if (not (eqv? (length (tuple)) 2))
(eopl:error 'flip
"Tuple is not length 2~%")
(cons (cdr tuple) (car tuple)))))
我尝试在 chez-scheme 中测试我的程序。当我在用法注释中使用测试用例时,出现此错误:Exception: attempt to apply non-procedure (a 1)
. 我以前从未使用过 Scheme,所以我非常感谢任何帮助和建议。谢谢!