Racket 因以下代码违反了合同:
(define (fringe x)
(append (car x) (fringe (cdr x))))
有什么想法有什么问题吗?
(car x)发生这种情况是因为没有返回一个列表(如果不知道错误的实际值很难确定x)。append是在两个列表之间定义的操作。如果要在列表的头部添加一个元素cons,请使用而不是append.
这就是我的意思:
(append 1 '(2 3))
=> append: expected argument of type <proper list>; given 1
(append '(1) '(2 3))
=> '(1 2 3)
(cons 1 '(2 3)) ; the recommended way!
=> '(1 2 3)