如何在ghci中跨多行定义一个函数?
我试图在 ghci 中定义跨多行的任何简单函数,以以下为例:
let abs n | n >= 0 = n
| otherwise = -n
到目前为止,我已尝试在第一行后按 Enter 键:
Prelude> let abs n | n >= 0 = n
Prelude> | otherwise = -n
<interactive>:1:0: parse error on input `|'
我还尝试使用 :{< /code> 和
:}
命令,但我没有走得太远:
Prelude> :{
unknown command ':{'
use :? for help.
我在 Linux 上使用适用于 Haskell 98 的 GHC Interactive 版本 6.6,我缺少什么?
I'm trying to define any simple function that spans multiple lines in ghci, take the following as an example:
let abs n | n >= 0 = n
| otherwise = -n
So far I've tried pressing Enter after the first line:
Prelude> let abs n | n >= 0 = n
Prelude> | otherwise = -n
<interactive>:1:0: parse error on input `|'
I've also attempted to use the :{
and :}
commands but I don't get far:
Prelude> :{
unknown command ':{'
use :? for help.
I'm using GHC Interactive version 6.6 for Haskell 98 on Linux, what am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
GHCi 现在具有多行输入模式,可通过 :set +m 启用。例如,
GHCi now has a multiline-input mode, enabled with :set +m. For example,
对于守卫(如您的示例),您可以将它们全部放在一行上并且它可以工作(守卫不关心间距)
如果您想使用与参数模式匹配的多个定义来编写函数,如下所示:
那么您将使用大括号和分号分隔定义
For guards (like your example), you can just put them all on one line and it works (guards do not care about spacing)
If you wanted to write your function with multiple definitions that pattern match on the arguments, like this:
Then you would use braces with semicolons separating the definitions
Dan 是正确的,但
:{
和:}
必须各自出现在自己的行上:这也与布局规则交互,因此使用 do-notation 时可能会更容易明确使用大括号和分号。例如,此定义失败:
但在添加大括号和分号时它会起作用:
这仅在从文件粘贴定义时才真正重要,其中缩进可能会改变。
Dan is correct, but
:{
and:}
must each appear on their own line:This also interacts with the layout rule, so when using do-notation it might be easier to use braces and semi-colons explicitly. For example, this definition fails:
But it works when braces and semi-colons are added:
This will only really matter when pasting definitions from a file, where indentation might change.
看起来
:{
和:}
是一个非常新的功能。您可能需要升级 GHC。编辑:确认,请参阅 http: //www.haskell.org/ghc/docs/6.8.2/html/users_guide/release-6-8-2.html
It looks like
:{
and:}
are a pretty new feature. You may need to upgrade GHC.Edit: confirmed, see http://www.haskell.org/ghc/docs/6.8.2/html/users_guide/release-6-8-2.html
如果您不想仅针对
:{
和:}
升级 GHC,则需要将其全部写在一行上:我不知道有任何Haskell 中的单个定义必须写在多行上。上面的内容确实在 GHCi 中有效:
对于其他表达式,例如
do
块,您需要使用带有大括号和分号的非布局语法(呃)。If you don't want to upgrade GHC just for
:{
and:}
, you'll need to write it all on one line:I'm not aware of any single definition in Haskell that must be written on multiple lines. The above does indeed work in GHCi:
For other expressions, such as
do
blocks, you'll need to use the non-layout syntax with curly braces and semicolons (eugh).看起来就像一次粘贴两行或对每个新行使用 control-enter 将其全部放在一起,至少在 https ://repl.it/languages/haskell。您将在第二行的开头看到两个点。或者将其放入文件中并:load 该文件(:l main)。为什么abs 不适用于负数?哦,你必须在数字两边加上括号。
It looks like pasting both lines at once or using control-enter for each new line keeps it all together, at least at https://repl.it/languages/haskell. You'll see 2 dots in the beginning of the second line. Or put it in a file and :load the file (:l main). How come abs doesn't work with negative numbers? Oh you have to put parentheses around the number.
我在 macOS Catalina 10.15.2 上使用 GHCi 版本 8.2.1。以下是我如何将函数类型声明和防护放在一起。请注意,左侧的垂直条表示 GHCi 多行。
I'm using GHCi, version 8.2.1 on macOS Catalina 10.15.2. The following is how I put both function type declaration and guards together. Note the vertical bars on the left are for GHCi multiple lines.