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.

有帮助吗?

解决方案

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))))
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top