简体   繁体   English

加载一个函数,但 ghci 似乎没有编译它

[英]Load a function but ghci doesnt seem to have compiled it

I have ran into this problems few times before but not sure what the cause is.我以前遇到过这个问题几次,但不确定是什么原因。 I just simply close the command window and reopen.我只是简单地关闭命令窗口并重新打开。 For ex, I enter u in the compiler and nothing happens (dont know how to explain this, better if you try it yourself).例如,我在编译器中输入u并且没有任何反应(不知道如何解释,最好自己尝试一下)。 Would be great if sb can explain me the problem and how to fix it!如果某人可以向我解释问题以及如何解决它,那就太好了!

u = let a = 2
        b = a + (let a = 3
                 in a+b)
    in b*b

Your function is pretty nonsensical, do you know that?你的函数非常荒谬,你知道吗? Lets do a little math, shall we.让我们做一个小数学,好吗?

module Q54272249 where

u :: Integer -- this tells you that `u` takes nothing and returns an Integer
u = let a = 2
        b = a + (let a = 3
                 in a+b)
    in b*b

Just substituting the inner a with the constant 3只需将内部a替换为常数3

u' :: Integer
u' = let a = 2
         b = a + (3+b)
     in b*b

Doing the same for the outer a对外部a做同样的事情

u'' :: Integer
u'' = let b = 2 + (3+b) in b*b

Addition is associative加法是结合的

u''' :: Integer
u''' = let b = 5+b in b*b

Replacing the let binding with an equivalent function用等效函数替换 let 绑定

u'''' :: Integer
u'''' = (5 + u'''') ^ 2

Do you see the problem now?你现在看到问题了吗? It is a function that takes no arguments and returns a value of type Integer that is 5 added to itself squared.它是一个不带参数的函数,它返回一个Integer类型的值,该值是5加上自身的平方。 What is the value of itself at some point in time?在某个时间点自身的价值是多少? You recursively evaluate it and never get an answer.你递归地评估它并且永远得不到答案。

We can fix that though.不过我们可以解决这个问题。 Make the function take an argument and establish a termination criterion like so.让函数接受一个参数并像这样建立一个终止条件。

u''''' :: Integer -> Integer
u''''' 0 = 0
u''''' x = (5 + u''''' (x - 1)) ^ 2

x gets smaller by 1 with every call and when it hits 0 it will return 0 or any other value you would like to. x每次调用都会变小1 ,当它达到0 ,它将返回0或您想要的任何其他值。 That is called recursion and it's always the same, establish one or more basecases and a recurrence relation.这就是所谓的递归,它总是相同的,建立一个或多个基本情况和递归关系。

Note that recurrence relations are the counterpart in discrete mathematics to differential equations in analysis.请注意,递推关系在离散数学中与分析中的微分方程相对应。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM