1

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 Answers 3

1
(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.
Sign up to request clarification or add additional context in comments.

1 Comment

+1 Good catch on the (null? lst) vs (null? fun-list) thing!
0

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

Comments

0

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)))))))

1 Comment

The function is supposed to work like (map (apply compose fun-list) lst).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.