One example in SICP
質問
This is an example in SICP, I enter it but it has an error.
(define (sum term a next b)
(if (> a b)
0
(+ (term a)
(sum term (next a) next b))))
This is the error:
function call: expected a function after the open parenthesis, but found a variable
解決
If you are using DrRacket, you may be running DrRacket under the "Beginner Student Language" mode. At the introductory CS level, that particular language level mode disables higher-order functions. SICP, on the other hand, assumes a language level that allows higher-order procedures.
Assuming this is the case, you can fix this by adjusting DrRacket so it uses a language level that allows higher-order procedures such as #lang racket.
See Choosing a Language: switch DrRacket over to "Use the language declared in the source", and make the top line of your program:
#lang racket
That should then let you write your function.
By the way there is specialized support for SICP for DrRacket; if you're going to go through SICP, you probably want to use this. You can enable this support by changing the top #lang line to:
#lang planet neil/sicp
It'll take a little time the first time you run a program with this, as it automatically installs the support software; subsequent use of the package should be fast.
他のヒント
I believe there's a problem with the way you're invoking the procedure. For example, this works fine with the above code:
(sum values 1 add1 10)
> 55