[英]Why does this result in an infinite loop [SICP]?
I am working on a problem from SICP.我正在解决 SICP 的一个问题。 I am not sure why this function results in an infinite while loop.
我不确定为什么这个 function 会导致无限循环。 Can someone please shed some light on this?
有人可以对此有所了解吗? I am sure there might be better ways to write this loop, but I am new to functional programming and still trying to wrap my head around writing loops like this.
我确信可能有更好的方法来编写这个循环,但我是函数式编程的新手,并且仍然试图围绕编写这样的循环。
(define (search-for-primes a b)
(if (> a b) (display " Done"))
((timed-prime-test a) (search-for-primes (+ a 1) b)))
The program keeps printing "Done" for this input: (search-for-primes 2 10), which makes no sense.该程序一直为这个输入打印“完成”:(search-for-primes 2 10),这是没有意义的。 'timed-prime-test' is a function which checks whether an input is a prime no.
'timed-prime-test' 是一个 function,它检查输入是否是素数。 or not.
或不。 Here's what I thinking: the program keeps calling itself until a = 11, at which point it would quit.
这就是我的想法:程序一直调用自己,直到 a = 11,此时它会退出。 However, even for the first 9 iterations (a=2 to a=10), I see no output from 'timed-prime-test' function and this indicates to me that the control is not even reaching the 2nd line, but then how is the function call happening infinite times?
然而,即使对于前 9 次迭代(a=2 到 a=10),我也没有看到来自“定时-prime-test”function 的 output,这表明控制甚至没有到达第二行,但是如何function 调用是无限次发生的吗?
Edit: I was looking at the syntax of 'if' in the book again and I noticed two things: a) 'alternative' is my code is outside the if block which is wrong.编辑:我再次查看书中“if”的语法,我注意到两件事:a)“替代”是我的代码在 if 块之外,这是错误的。 I fixed this but the program output for a = 10. b) A footnote says that it should only have single expressions.
我解决了这个问题,但程序 output 用于 a = 10。b)脚注说它应该只有单个表达式。 Does this behavior of my program has to do with b)?
我的程序的这种行为是否与 b) 有关? If yes, how does the program execution happen in this case?
如果是,在这种情况下程序执行是如何发生的?
If you format your code properly the answer will be immediately clear.如果您正确格式化代码,答案将立即清晰。 This is why formatting your code properly is important .
这就是正确格式化代码很重要的原因。 Here's your definition with better formatting:
这是您的定义,格式更好:
(define (search-for-primes a b)
(if (> a b)
(display " Done"))
((timed-prime-test a) (search-for-primes (+ a 1) b)))
So, (search-for-primes 1 1)
:所以,
(search-for-primes 1 1)
:
((timed-prime-test a) (search-for-primes (+ a 1) b))
, to do which:((timed-prime-test a) (search-for-primes (+ a 1) b))
,这样做:
(timed-prime-test 1)
and (search-for-primes 2 1)
in some order, which at least means evaluating (search-for-primes 1 1)
, which:(timed-prime-test 1)
和(search-for-primes 2 1)
,这至少意味着评估(search-for-primes 1 1)
,其中:
Now it should be obvious what the problem is here.现在应该很明显问题出在哪里了。
((timed-prime-test a) (search-for-primes (+ a 1) b))
The current continuation of the recursive call of (search-for-primes (+ a 1) b)
is ((timed-prime-test a) _)
but it never reaches the _
because you never fill its value, ie the value of _
is a bottom type
. (search-for-primes (+ a 1) b)
的递归调用的当前延续是((timed-prime-test a) _)
但它永远不会到达_
因为你永远不会填充它的值,即_
是bottom type
。 So also the value of ((timed-prime-test a) _)
is a bottom.所以
((timed-prime-test a) _)
的值也是一个底部。
But the value of search-for-primes
is the value of ((timed-prime-test a) _)
.但是
search-for-primes
的值是((timed-prime-test a) _)
的值。 So it has no value.所以它没有价值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.