如何在ghci中跨多行定义一个函数?

发布于 2024-09-02 03:12:46 字数 525 浏览 5 评论 0原文

我试图在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

热血少△年 2024-09-09 03:12:46

GHCi 现在具有多行输入模式,可通过 :set +m 启用。例如,

Prelude> :set +m
Prelude> let fac 0 = 1
Prelude|     fac n = n * fac (n-1)
Prelude|
Prelude> fac 10
3628800

GHCi now has a multiline-input mode, enabled with :set +m. For example,

Prelude> :set +m
Prelude> let fac 0 = 1
Prelude|     fac n = n * fac (n-1)
Prelude|
Prelude> fac 10
3628800
段念尘 2024-09-09 03:12:46

对于守卫(如您的示例),您可以将它们全部放在一行上并且它可以工作(守卫不关心间距)

let abs n | n >= 0 = n | otherwise = -n

如果您想使用与参数模式匹配的多个定义来编写函数,如下所示:

fact 0 = 1
fact n = n * fact (n-1)

那么您将使用大括号和分号分隔定义

let { fact 0 = 1 ; fact n = n * fact (n-1) }

For guards (like your example), you can just put them all on one line and it works (guards do not care about spacing)

let abs n | n >= 0 = n | otherwise = -n

If you wanted to write your function with multiple definitions that pattern match on the arguments, like this:

fact 0 = 1
fact n = n * fact (n-1)

Then you would use braces with semicolons separating the definitions

let { fact 0 = 1 ; fact n = n * fact (n-1) }
我的影子我的梦 2024-09-09 03:12:46

Dan 是正确的,但 :{:} 必须各自出现在自己的行上:

> :{ 
> let foo a b = a +
>           b
> :}
> :t foo
foo :: (Num a) => a -> a -> a

这也与布局规则交互,因此使用 do-notation 时可能会更容易明确使用大括号和分号。例如,此定义失败:

> :{
| let prRev = do
|   inp <- getLine
|   putStrLn $ reverse inp
| :}
<interactive>:1:18:
    The last statement in a 'do' construct must be an expression

但在添加大括号和分号时它会起作用:

> :{
| let prRev = do {
|   inp <- getLine;
|   putStrLn $ reverse inp;
| }
| :}
> :t prRev
prRev :: IO ()

这仅在从文件粘贴定义时才真正重要,其中缩进可能会改变。

Dan is correct, but :{ and :} must each appear on their own line:

> :{ 
> let foo a b = a +
>           b
> :}
> :t foo
foo :: (Num a) => a -> a -> a

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:

> :{
| let prRev = do
|   inp <- getLine
|   putStrLn $ reverse inp
| :}
<interactive>:1:18:
    The last statement in a 'do' construct must be an expression

But it works when braces and semi-colons are added:

> :{
| let prRev = do {
|   inp <- getLine;
|   putStrLn $ reverse inp;
| }
| :}
> :t prRev
prRev :: IO ()

This will only really matter when pasting definitions from a file, where indentation might change.

永不分离 2024-09-09 03:12:46

看起来 :{:} 是一个非常新的功能。您可能需要升级 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

看透却不说透 2024-09-09 03:12:46

如果您不想仅针对 :{:} 升级 GHC,则需要将其全部写在一行上:

> let abs' n | n >= 0 = n | otherwise = -n

我不知道有任何Haskell 中的单个定义必须写在多行上。上面的内容确实在 GHCi 中有效:

> :t abs'
abs' :: (Num a, Ord a) => a -> a

对于其他表达式,例如 do 块,您需要使用带有大括号和分号的非布局语法(呃)。

If you don't want to upgrade GHC just for :{ and :}, you'll need to write it all on one line:

> let abs' n | n >= 0 = n | otherwise = -n

I'm not aware of any single definition in Haskell that must be written on multiple lines. The above does indeed work in GHCi:

> :t abs'
abs' :: (Num a, Ord a) => a -> a

For other expressions, such as do blocks, you'll need to use the non-layout syntax with curly braces and semicolons (eugh).

千纸鹤带着心事 2024-09-09 03:12:46

看起来就像一次粘贴两行或对每个新行使用 control-enter 将其全部放在一起,至少在 https ://repl.it/languages/haskell。您将在第二行的开头看到两个点。或者将其放入文件中并:load 该文件(:l main)。为什么abs 不适用于负数?哦,你必须在数字两边加上括号。

   let abs n | n >= 0 = n 
..           | otherwise = -n
   abs (-1)

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.

   let abs n | n >= 0 = n 
..           | otherwise = -n
   abs (-1)
灯角 2024-09-09 03:12:46

我在 macOS Catalina 10.15.2 上使用 GHCi 版本 8.2.1。以下是我如何将函数类型声明和防护放在一起。请注意,左侧的垂直条表示 GHCi 多行。

λ: let abs' :: (Num a, Ord a) => a -> a
 |     abs' n | n >= 0 = n | otherwise = -n
 | 
λ: abs' 7
7
λ: abs' (-7)
7

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.

λ: let abs' :: (Num a, Ord a) => a -> a
 |     abs' n | n >= 0 = n | otherwise = -n
 | 
λ: abs' 7
7
λ: abs' (-7)
7
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文