This is actually extremely easy if you use higher-order functions like map
, especially since Scheme's map
can accept many arguments, in which case it acts like a "zip" function. This means it's possible to implement your list-mins
function in a single, terse line of code:
(define (list-mins . lsts)
(apply map min lsts))
This uses some relatively complex machinery of Scheme/Racket, though, so it might not be very clear what's going on. The dotted argument, paired with apply
allows list-mins
to take any number of lists, but you really only need it to accept two, so here's a simpler version:
(define (list-mins a b)
(map min a b))
What does this do? Well, map
iterates over its arguments in parallel, applies a procedure to each set of elements, then produces a new list with the elements of the results. The min
function simply returns the minimum of its arguments. To see what this looks like in action, here's what the above code is basically doing:
(map min '(1 7 5) '(2 8 3))
=> (list (min 1 2)
(min 7 8)
(min 5 3))
=> (list 1 7 3)
Of course, it's possible to write this yourself, too, using recursion. Doing it manually would look like this:
(define (list-mins a b)
(if (empty? a)
'()
(cons (min (first a) (first b))
(list-mins (rest a) (rest b)))))
This is pretty much just unrolling what map
does, and using map
directly is much more clear (it expresses your intent to iterate over a set of lists), so it would be much more idiomatic than doing the recursion yourself. Still, if you're learning, then the explicit version might be more clear.