This is homework, so I don’t want the answer. I only need a push in the right direction. I am required to map multiple functions onto a list. For instance:
(map-multi (list plus-one square) '(4 5 6)) => (25 36 49)
I am able to have it map the first function to the elements of the list, however, I get very lost after that. Also, since this is introductory, I am limited to introductory functions (const, append, car, cdr, member, etc.)
(define (map-multi f l)
(cond
((null? l)
l)
(else (cons ((car f) (car l))
(map-multi f (cdr l))))))
You need to compose the functions you receive in the
fparameter. For simplicity’s sake, let’s say that there are only two functions in the list – then you need to apply the first function to the current element in the list of numbers and then apply the second function to the result of that. If you can use thecomposeprocedure go ahead with it and change this line in your code:… with this one:
If you can’t use
compose, then replace the same line with this one:Now, if the problem is more general and you’re required to map a list of functions with more than two elements, then once more replace the same line in your code with this:
And implement a helper function that composes and returns all the functions in the list, by successive calls to
compose. This one is left as an exercise for you, given that it’s homework – but if you understand how the above code works for just two functions, it should be easy enough to extend the result for a list of multiple functions:Notice that the identity function is required for handling the case where there are no elements in the list of functions; it’s very simple to define, it just returns the same value that was passed as parameter.
Also be aware that
compose-multireturns a function, the result of composing all the functions in the list –composedoes this for you, but if you’re not allowed to use it just remember that this:… is equivalent to this: