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

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

问题描述

我在输入ghci中的多行命令时遇到问题。
以下两行代码可以从文件中找到:

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

但是当我输入ghci时, 。我也尝试将代码放入:{...:} 中,但它们对于此示例也不起作用,因为这只是将行附加到一行中,不应该是这样。



我使用WinGHCi,版本2011.2.0.1

解决方案大多数时候,你可以依靠类型推断来为你制定一个签名。在你的例子中,以下就足够了:

  Prelude>让addTwo xy = x + y 

如果您确实需要使用类型签名或您的定义跨越多行,你可以在ghci中做到这一点:

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

请注意,您也可以将它压缩到一行: p>

  Prelude>让addTwo :: Int  - > Int  - > Int; addTwo xy = x + y 

你可以在文档提示符中的交互式评估。


I am having problem in entering multi-line commands in ghci. The following 2-line code works from a file:

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

But when I enter in ghci, I get errors. 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.

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

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

You can find out more about interacting with ghci on the Interactive evaluation at the prompt section of the documentation.

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

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