The trick is knowing what to pass as a function parameter, here's how:
(define (func l1 l2)
(foldr (lambda (e1 e2 acc)
(cons (list e1 e2) acc))
'()
l1 l2))
Notice that we're passing two lists at the end of foldr
, so the lambda
expects three parameters: the current element from the first list (e1
), the current element from the second list (e2
) and the accumulated output (acc
), which starts with value '()
. The rest is easy, just build the output along using cons
and list
. It works as expected:
(func '(1 2 3) '(4 5 6))
=> '((1 4) (2 5) (3 6))