通过 Emacs 评估 ghci 或 Hugs 中的缓冲区
在 Emacs 中使用 sml-mode,我已经能够使用 Cc Cb
将缓冲区内容直接发送到下级 SML 进程。现在我只想用 Haskell 做同样的事情。 Haskell 模式似乎不支持这一点,所以我想知道:使用 Emacs 和 Haskell 解决这个问题的正确方法是什么?
在学习 SML 时,我几乎不间断地使用 Cc Cb
来轻松评估我的程序,立即看到赋值等的结果。但是如果我使用 Cc Cl
code> 在 haskell 模式下,在包含两行的已保存文件上,let foo = "foo"
和 let bar = "bar"
- 我收到 "parse error (可能不正确缩进)”
Using sml-mode in Emacs I have been able to send my buffer contents directly to an inferior SML process using C-c C-b
. Now I want to do the same thing only with Haskell. Haskell-mode does not seem to support this, so I'm wondering: What is the right way to go about this with Emacs and Haskell?
While learning SML I've been using C-c C-b
almost non-stop to easily evaluate my program as I go, instantly seeing the results of assigning values etc. But if I use C-c C-l
in haskell-mode on a saved file containing two lines, let foo = "foo"
and let bar = "bar"
- I get "parse error (possibly incorrect indentation)"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你犯了一个常见的菜鸟错误,混淆了你可以在 ghci 的 repl 中编写的内容和你在 haskell 源文件中编写的内容。
所有 sml 解释器都是以这样的方式制作的,您可以将任何顶级声明写入 repl,或者换句话说:您可以在 sml 文件中写入的任何内容,都可以写入 sml 解释器。因此,您可以将
val foo = "bar"
写入文件并使用Cc Cb
加载该文件,并且您可以只输入val foo = "bar"
进入解释器。另一方面,由于 haskell 的工作方式,您可以将
let foo = 42
写入 ghci,但它不是有效的顶级声明,因此不能出现在 haskell 源文件中(靠它自己)。另一方面,您可以在 haskell 源文件中包含 id n = n 并使用 Cc Cl 加载该文件,但是您不能将其直接写入 ghci (您将收到错误::1:6: 输入“=”时解析错误)。原因是 ghci 中的 repl 在 IO monad 中运行,因此写入 ghci 的内容必须使用 do 表示法来完成。我只能建议您阅读提示下的交互式评估使用 GHCi 用户指南。sml 模式下的 Cc Cb 与 haskell 模式下的 Cc Cl 完全相同,至少在概念上是这样。我对 haskell 模式的内部了解不多,但在 sml 模式下
Cc Cb
在解释器中执行一些 sml 代码,通常是use(...)
代码>函数。在 haskell 模式下,它似乎只是执行:load "..."
ghci 命令I think you are making a common rookie mistake, confusing what you can write inside the repl of ghci and what you write in a haskell source file.
All sml interpreters are made in such a way that you can write any top level declaration into the repl, or put in other words: anything you can write in an sml file, you can write into the sml interpreter. Thus you are allowed to write
val foo = "bar"
into a file and useC-c C-b
to load the file and you are allowed to just put inval foo = "bar"
into the interpreter.On the other hand, because of how haskell work, you can write
let foo = 42
into ghci, however it is not a valid top level declaration and thus this can't be in a haskell source file (by it self). On the other hand you can haveid n = n
in a haskell source file and useC-c C-l
to load the file, however you can't write this directly into ghci (you will get an error: :1:6: parse error on input '='). The reason for this is that the repl in ghci runs in the IO monad, and thus what ever you write into ghci must be done using the do notation. I can only recommend that you read Interactive evaluation at the prompt from the Using GHCi user guide.C-c C-b
in sml-mode is the exact same thing asC-c C-l
in haskell-mode, well atleast conceptually. I don't know that much about the internals of haskell-mode, but in sml-modeC-c C-b
executes some sml code in the interpreter, normally theuse(...)
function. In haskell-mode it seems to just excute the:load "..."
ghci command您不能使用 ghci(或拥抱)来做到这一点,原因很简单,您不能在 ghci(或拥抱)中编写顶级定义。因此,即使您手动将文件内容粘贴到 ghci 中,您得到的也只是语法错误。
因此,使用 Cc Cl 加载文件是最好的选择。
You can't do that with ghci (or hugs) for the simple reason that you can't write top-level definitions in ghci (or hugs). So even if you manually pasted your file's contents into ghci, all you'd get would be a syntax error.
So loading a file using C-c C-l is the best you can do.