2012-10-27 69 views
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”?

回答

6

事实上,正如您所说:高级语言不支持内部define s。为了使用SICP练习,我被告知最好使用neil/sicp包:instructions for using this are detailed here

然而,即使是标准的Racket语言(#lang racket)也会支持内部的define,没有任何问题。

+4

Chris是对的,在#lang racket和neil/sicp中都支持内部定义。如果你喜欢使用高级教学语言(步进器是一个很好的工具!),那么你可以使用'本地'做内部又名局部定义。请参阅http://docs.racket-lang.org/htdp-langs/advanced.html?q=local#(form._((lib._lang/htdp-advanced..rkt)._local)) – soegaard