我一直在做一些功课,写了一些代码,实际上找不到它不工作的原因。这部分工作的主要思想是创建一个流,给我一个给定X(角度我猜)泰勒级数余弦函数的元素。反正这里是我的代码,我会很高兴,如果有人能指出我的原因,这是行不通的:)泰勒系列计划流
(define (force exp) exp)
(define (s-car s) (car s))
(define (s-cdr s) (force (cdr s)))
; returns n elements of stream s as a list
(define (stream->list s n)
(if (= n 0)
'()
(cons (s-car s) (stream->list (s-cdr s) (- n 1)))))
; returns the n-th element of stream s
(define stream-ref (lambda (s n)
(if (= n 1)
(s-car s)
(stream-ref (s-cdr s) (- n 1)))))
; well, the name kinda gives it away :) make factorial n!
(define (factorial x)
(cond ((= x 0) 1)
((= x 1) 1)
(else (* x (factorial (- x 1))))))
; this function is actually the equation for the
; n-th element of Taylor series of cosine
(define (tylorElementCosine x)
(lambda (n)
(* (/ (expt -1 n) (factorial (* 2 n))) (expt x (* 2 n)))))
; here i try to make a stream of those Taylor series elements of cosine
(define (cosineStream x)
(define (iter n)
(cons ((tylorElementCosine x) n)
(lambda() ((tylorElementCosine x) (+ n 1)))))
(iter 0))
; this definition should bind cosine
; to the stream of taylor series for cosine 10
(define cosine (cosineStream 10))
(stream->list cosine 10)
; this should printi on screen the list of first 10 elements of the series
但是,这并不工作,我不知道为什么。
我使用Dr.Scheme 4.2.5,并将语言设置为“Essentials of Programming Languages 3rd ed”。
你认为“它不工作”是一个准确的失败描述?它究竟如何不起作用?你试过调试它吗? – Grizzly 2012-01-10 17:49:56
是的我没有,一旦代码存在,这不是一个失败,90%的工作就像一个魅力,只有2个逻辑错误存在...没有dis-mate :) – 2012-01-10 18:14:24