问题
SCHEME/Racket/R5RS
Attempting to make a recursive procedure that pairs 2 lists of the same size. Just cant get the recursive call right. This is what I have and I am stuck.
(define (pairs list1 list2)
(if (or (null? list1) (null? list2))
'()
(cons (car list1) (car list2))
))
Test Case: (pairs '(1 2 3) '(a b c)) Desired Output: ((1 . a) (2 . b) (3 . c)) Current Output: (1 . a)
回答1:
You just have to cons
the current result to the recursive call of the procedure, and that's it!
(define (pairs list1 list2)
(if (or (null? list1) (null? list2))
'()
(cons (cons (car list1) (car list2))
(pairs (cdr list1) (cdr list2)))))
回答2:
Would this be an acceptable solution as well?
(define pairs
(lambda (x y)
(map cons x y)))
来源:https://stackoverflow.com/questions/40538638/pairing-2-lists-scheme