这就是我写的:
(: mmm : (Listof Any) (Listof Any) -> (Listof Any))
(define (mmm list1 list2)
(cond [(or (null? list1) (null? list2)) null]
(and (cons (first list1) (first list2)) (mmm (rest list1) (rest list2)))))
我会举个例子:
list1: a b c
list2: 1 2 3
answer: ((a 1) (b 2) (c 3))
编辑 它们的大小相同
答案 0 :(得分:2)
如果您被允许使用map
,则可以使用:
(define (mmm lst1 lst2) (map (lambda (x y) (list x y)) lst1 lst2))
答案 1 :(得分:1)
您的代码存在一些问题:
else
开头(因为它是最后一个且条件是互斥的),而不是and
cons
调用递归的结果要修复它们,请尝试此操作 - 假设输入列表长度相等:
(define (mmm list1 list2)
(cond [(or (null? list1) (null? list2)) null]
[else (cons (list (first list1) (first list2))
(mmm (rest list1) (rest list2)))]))
现在程序按预期工作:
(mmm '(a b c ) '(1 2 3))
=> '((a 1) (b 2) (c 3))
答案 2 :(得分:1)
如果您知道列表大小相同,那么它就变得简单:
(define (mmm list1 list2) (map list list1 list2))
注意:如果您真正想要的话,请将map list
替换为map cons
。您的代码使用了cons
,但您的结果示例表明list
就是您想要的。