1
votes

I am supposed to define a function that takes as arguments a list of functions and another list and returns a list of values obtained by applying all the functions, in sequence, over the elements of the list.

I came up with the following but I receive the error:

+: expects type <number> as 1st argument, given: (1 2 3); other arguments were: 1

when I try to use the function with the sample input, (map-many (list (lambda (x) (+ x 1)) (lambda (x) (* x x))) '(1 2 3)). Any suggestions would be appreciated.

(define (map-many fun-list lst)
    (if (null? lst) lst
        (map ((car fun-list) lst)
             (map-many (cdr fun-list) lst))))
3

3 Answers

2
votes
(define (map-many fun-list lst)
  (if (null? fun-list) lst
      (map (car fun-list)
           (map-many (cdr fun-list) lst))))

Your errors were:

  1. You finish your recursion on (null? lst) instead of (null? fun-list).
  2. The rest of the function was not logical.
1
votes

You're passing the wrong function to map. Instead of passing ((car fun-list) lst), try passing just (car fun-list).

0
votes

Your wording is a bit unclear to me. Are you supposed to do the following? Have two lists, one of procedures (let's call this list P), another with values (let's call this list V). So you're looking to then find a list where:

  • list-ref 0 = (P0 V0)
  • list-ref 1 = (P1 (P0 V1))
  • list-ref 2 = (P2 (P1 (P0 V2))

and so on?

(define (map-many procs vals)
   (let ((applied (map (car procs) vals))
     (if (null? vals)
         vals
         (cons (car applied) (map-many (cdr procs) (cdr applied)))))))