質問

I'm new to functional language and I'm doing SICP programming assignments using Racket.

Below is a snippet of code, and Racket informs me that define: expected only one expression for the function body, but found 5 extra parts, in line 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))))

I guess this problem is related to the language settings, but I already use "advanced" option.

Anybody know how to configure Racket properly, or internal "define" is not supported?

役に立ちましたか?

解決

Indeed, it's as you say: internal defines are not supported by the Advanced language. For working with the SICP exercises, I've been told it's best to use the neil/sicp package: instructions for using this are detailed here.

However, even the standard Racket language (#lang racket) will support internal defines without problems.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top