3
我是功能语言的新手,我正在使用Racket进行SICP编程任务。球拍是否支持内部“定义”?
下面的代码片段,以及球拍告诉我说define: expected only one expression for the function body, but found 5 extra parts
,在第5行((define (y k)
):
(define (simpson f a b n)
(define h (/ (- b a) n))
(define (y k)
(f (+ a (* k h))))
(define (factor k)
(cond ((or (= k 0) (= k n))
1)
((odd? k)
4)
(else
2)))
(define (term k)
(* (factor k)
(y k)))
(define (next k)
(+ k 1))
(if (not (even? n))
(error "n can't be odd")
(* (/ h 3)
(sum term (exact->inexact a) next n))))
我想这个问题是关系到语言设置,但我已经用“先进”选项。
任何人都知道如何正确配置Racket,或者不支持内部“define”?
Chris是对的,在#lang racket和neil/sicp中都支持内部定义。如果你喜欢使用高级教学语言(步进器是一个很好的工具!),那么你可以使用'本地'做内部又名局部定义。请参阅http://docs.racket-lang.org/htdp-langs/advanced.html?q=local#(form._((lib._lang/htdp-advanced..rkt)._local)) – soegaard