泰勒级数的方案流
我一直在做一些作业,写了一些代码,但实际上找不到它不起作用的原因。这部分工作的主要思想是创建一个流,该流将为我提供给定 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,语言设置为“编程语言要点第三版”。
I've been doing some homework, wrote some code and can't actually find the reason why it doesn't work. The main idea of this part of the work is to make a stream that will give me elements of Taylor series of cosine function for a given X (angle i guess). anyways here is my code, I'd be happy if some one could point me to the reasons it doesn't work :)
(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
However, this doesn't work, and I don't know why.
I'm using Dr.Scheme 4.2.5 with the language set to "Essentials of Programming Languages 3rd ed".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为我感觉很好(并且对方案怀旧),所以我实际上仔细检查了您的代码以找到错误。据我所知,有两个问题使代码无法正常运行:
如果我正确理解你的代码
(force exp)
应该评估exp
,但是你直接返回它(未评估)。所以它可能应该定义为(define (force exp) (exp))
第二个问题是在你的 lambda 中:
(lambda() ((tylorElementCosine x) (+ n 1)) )
将计算泰勒级数的下一个元素,而它应该计算为流。您可能想要这样的东西:(lambda() (iter (+ n 1)) )
我还没有检查输出是否正确,但通过这些修改,它至少可以运行。因此,如果代码还有任何问题,应该在所使用的公式中。
不过,我建议下次您在作业上需要帮助时,至少告诉我们问题到底出现在哪里以及您已经尝试过什么(社区确实不赞成“这里有一些代码,请帮我修复它”之类的问题)。
Since I was feeling nice (and nostalgic about scheme) I actually waded through your code to finde the mistakes. From what I can see there are 2 problems which keeps the code from running as it should:
If I understand your code correctly
(force exp)
should evaluateexp
, however you directly return it (unevaluated). So it probably should be defined as(define (force exp) (exp))
The second problem is in your lambda:
(lambda() ((tylorElementCosine x) (+ n 1)) )
will evaluate to the next element of the taylor series, while it should evaluate to a stream. You probably want something like this:(lambda() (iter (+ n 1)) )
I haven't checked if the output is correct, but with those modifications it does at least run. So if there are any more problems with the code the should be in the formula used.
However I'd suggest that next time you want help with your homework you at least tell us where exactly the problem manifests and what you tried already (the community does frown on "here is some code, please fix it for me" kind of questions).