GHCi 中的多行命令 [英] Multi-line commands in GHCi

查看:32
本文介绍了GHCi 中的多行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ghci 中输入多行命令时遇到问题.

I am having problem in entering multi-line commands in ghci.

以下 2 行代码适用于文件:

The following 2-line code works from a file:

addTwo :: Int -> Int -> Int
addTwo x y = x + y

但是当我输入ghci时,出现错误:

But when I enter in ghci, I get an error:

<interactive>:1:1: error:
    Variable not in scope: addTwo :: Int -> Int -> Int

我也尝试将代码放在 :{ ... :} 中,但它们也不适用于此示例,因为这只是将行附加到一行中,这不应该情况.

I also tried putting the code inside :{ ... :}, but they are also not working for this example, because this is just appending the lines into one line, which should not be the case.

我使用的是 WinGHCi,版本 2011.2.0.1

I am using WinGHCi, version 2011.2.0.1

推荐答案

大多数时候,您可以依靠类型推断来为您制定签名.在您的示例中,以下内容就足够了:

Most of the time, you can rely on type inference to work out a signature for you. In your example, the following is sufficient:

Prelude> let addTwo x y = x + y

如果你真的想要一个带有类型签名的定义,或者你的定义跨越多行,你可以在 ghci 中做到这一点:

If you really want a definition with a type signature, or your definition spans over multiple lines, you can do this in ghci:

Prelude> :{
Prelude| let addTwo :: Int -> Int -> Int
Prelude|     addTwo x y = x + y 
Prelude| :}
Prelude> addTwo 4 7
11

请注意,您也可以将其压缩到一行中:

Note that you can also squeeze this onto one line:

Prelude> let addTwo :: Int -> Int -> Int ; addTwo x y = x + y

您可以在 交互式评估中找到有关与 ghci 交互的更多信息在文档的提示部分.

这篇关于GHCi 中的多行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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