Pab*_*ado 0 scheme list racket
我正在尝试将Racket上的两个列表相交,但是以下代码不起作用:
(define (intersection a b)
(if (null? a)
'()
(if (contains (car a) b)
(cond (car a)(intersection (cdr a) b))
(intersection (cdr a) b))))
Run Code Online (Sandbox Code Playgroud)
结果总是以列表b中包含的列表a的第一项加上列表a的其余部分结束。例如:
a = '(1 2 3 4 5 6)
b = '( 10 20 4 30 33)
Run Code Online (Sandbox Code Playgroud)
将返回:
'(4 5 6)
Run Code Online (Sandbox Code Playgroud)
我确定包含内容正确运行,因此错误必须出在我发布的代码中。谢谢!
嘿,如果您不想这样做,可以使用Racket的set-intersect函数
(set-intersect '(1 2 3 4 7) '(3 4 5 6)) ; => '(4 3)
Run Code Online (Sandbox Code Playgroud)