Question

I am trying to write a function that calls the map function N times on a list. Its a pretty simple problem:

(define (mapNtimes f x L)
  (if (= x 0)
        L
        (mapNtimes (f (- x 1) map (f L)))))

Dr Racket is gives me the following error when I try and call my function. Using a statement like this:

(mapNtimes ((lambda (x) (* 2 x))) 2 '(1 2 3))

It gives me this error

    expected: number?
    given: '(3 5 1)
    argument position: 2nd
    other arguments...:
    2

I know it something to do with the syntax but I can't for the life of me figure out what it is. Would appreciate some help.

Was it helpful?

Solution

Perhaps you are new to Scheme and not familiar with the syntax for invoking Scheme functions.

Given a function f, you invoke it using:

(f arg1 arg2)

not

f (arg1 arg2)

This works for me.

(define (mapNtimes f x L)
  (if (= x 0) L
    (mapNtimes f (- x 1) (map f L))))
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top