它从文件加载时起作用,但在输入ghci时不起作用。为什么? [英] It works when loaded from file, but not when typed into ghci. Why?

查看:81
本文介绍了它从文件加载时起作用,但在输入ghci时不起作用。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将以下两行放入foobar.hs中:
$ b $

If I put the following 2 lines into foobar.hs

f 1 = 1
f x = f (x-1)

然后

then

$ ghci
> :load foobar.hs
> f 5
1

但是如果我这样做的话

but if I do

$ ghci
> let f 1 = 1
> let f x = f (x-1)
> f 5
^CInterrupted.

然后它不会返回。为什么?

then it does not return. Why?

推荐答案

后者绑定覆盖前者。在ghci中使用它:

The latter binding overrides the former. Use this in ghci:

Prelude> :{
Prelude| let f 1 = 1
Prelude|     f x = f (x-1)
Prelude| :}
Prelude> f 5
1

或者,没有布局:

Or, without the layout:

Prelude> let f 1 = 1; f x = f (x-1)
Prelude> f 5
1

这篇关于它从文件加载时起作用,但在输入ghci时不起作用。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆