简体   繁体   English

为什么这会导致无限循环 [SICP]?

[英]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)

  • tests if 1 is more than 1... it's not;测试 1 是否大于 1……不是;
  • and then starts to evaluate ((timed-prime-test a) (search-for-primes (+ a 1) b)) , to do which:然后开始评估((timed-prime-test a) (search-for-primes (+ a 1) b)) ,这样做:
    • evaluates (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) ,其中:
      • tests if 2 is more than 1... it is测试 2 是否大于 1... 它是
        • so it prints "Done";所以它打印“完成”;
      • and then starts to evaluate... oops.然后开始评估……哎呀。

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.

相关问题 为什么这个递归阶乘会导致无限递归? - Why does this recursive factorial lead to infinite recursion? 为什么foldright适用于无限列表? - Why does foldright work for infinite lists? 如何使用Y-Combinator; 为什么这个无限递归返回9? - How to use Y- Combinator; why does this infinite recursion return 9? 为什么python过滤器处理无限序列时不会溢出? - Why does the python filter do not overflow when it processes an infinite sequence? 为什么这个Haskell代码能够成功地使用无限列表? - Why does this Haskell code work successfully with infinite lists? 为什么第一个Haskell函数FAIL处理无限列表,而第二个片段SUCCEEDS带有无限列表? - Why does this first Haskell function FAIL to handle infinite lists, while this second snippet SUCCEEDS with infinite lists? 函数式编程中的无限循环? - infinite loop in functional programming? 无限循环与计数器在不老长寿 - Infinite loop with counter in elixir 陷入函数的无限循环中 - Stuck in an infinite loop in a function Haskell 中的“修复”是什么? 为什么“修复错误”会打印一个无限的字符串? 为什么“take 10 $ fix error”也一样呢? - What is "fix" in Haskell? And why does "fix error" print an infinite string? And why also "take 10 $ fix error" does the same too?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM