在确定之前使用符号

Tri*_*tos 6 haskell

如何在方程式右侧下面的行中使用符号"fibs",尽管它还没有定义:

let fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
Run Code Online (Sandbox Code Playgroud)

Dan*_*her 19

关键在于定义 fibs

fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
Run Code Online (Sandbox Code Playgroud)

在其他地方使用之前不会进行评估.然后使用已知部分展开定义.我们一开始fibs = 0 : 1 : ???.然后,如果需要第三个元素,则进一步评估定义,

fibs = 0 : 1 : zipWith (+) (0 : 1 : ???) (tail (0 : 1 : ???))
     = 0 : 1 : zipWith (+) (0 : 1 : ???) (1 : ???)
     = 0 : 1 : (0 + 1) : zipWith (+) (1 : ???) (???)
Run Code Online (Sandbox Code Playgroud)

然后,未知的部分???已经部分已知,已经被确定为??? = 1 : ????,所以展开可以继续,

     = 0 : 1 : 1 : zipWith (+) (1 : 1 : ????) (1 : ????)
     = 0 : 1 : 1 : 2 : zipWith (+) (1 : ????) (????)
     -- now ???? is known to be 2:?????
     = 0 : 1 : 1 : 2 : zipWith (+) (1 : 2 : ?????) (2 : ?????)
Run Code Online (Sandbox Code Playgroud)

等等


GMa*_*ckG 6

它实际上不会尝试调用fibs您的定义,直到fibs您的程序中的其他内容稍后使用,此时fibs已完全定义.

您也可以使用大多数其他语言执行此操作:

int foo(int x)
{
    if (x <= 0) return 0;

    // will call foo when it gets there, at which point its been defined
    foo(x - 1); 
}
Run Code Online (Sandbox Code Playgroud)