帮助我理解 Haskell ghci 配置文件中的函数

发布于 2024-10-03 21:43:44 字数 443 浏览 0 评论 0原文

我的 xp 盒子使用 Haskell Platform,版本 6.12.3

我在 .ghci 文件中包含以下内容。

:def test \_ -> return $ ":load c:\scripts\haskell\test.hs"

现在我可以通过输入 :test 来编译并加载 test.hs 脚本。不过,我也可以使用 :t、:te 或 :tes。我认为这是 Haskell 的习惯用法,称为模式匹配。不幸的是,我的 :test 命令现在阻止使用 :t 作为 Haskell 命令 :type 的缩写形式。

如何调用以“t”开头的整个单词(例如“test”)的函数,而不会干扰 :type:t 别名?

My xp box uses Haskell Platform, version 6.12.3

I include the following in my .ghci file.

:def test \_ -> return $ ":load c:\scripts\haskell\test.hs"

Now I can compile and load my test.hs script by entering :test. However I can also use :t, :te or :tes. I presume this is the Haskell idiom called pattern matching. Unfortunately, my :test command now prevents the use of :t as the short form of the Haskell command :type.

How can I invoke a function with an entire word that starts with a "t", such as "test" that won't interfere with the :t alias of :type?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

江湖正好 2024-10-10 21:43:44

您可以使用 :def 为命令创建别名。

:def t return . (":type "++)

如何调用以“t”开头的整个单词(例如“test”)的函数,而不会干扰 :type< 的 :t 别名/代码>?


到目前为止,我们一直在讨论 GHCi 命令。函数名称不以冒号开头,并且部分函数名称不会自动完成。

You can create an alias for a command with :def.

:def t return . (":type "++)

How can I invoke a function with an entire word that starts with a "t", such as "test" that won't interfere with the :t alias of :type?

So far we've been talking about GHCi commands. Function names don't start with a colon and there's no auto-completion of partial function names.

幻想少年梦 2024-10-10 21:43:44

在您的问题中,您混淆了两个独立的事物:GHCi 命令,它以冒号开头,允许您在运行时与 GHCi 进行交互;和 Haskell 函数,它们具有普通名称,可以在 GHCi 中运行(但一般来说不能查询它)。任何 Haskell 教程都会帮助您处理普通函数;对于 GHCi 命令,最好的参考可能是 GHC 手册GHCi 部分;甚至还有一个关于命令的小节

我认为散热器的建议 最适合恢复 :t 的使用; GHCi 试图通过找出命令的前缀并运行它来帮助您,所以如果它猜错了,您只需告诉它您的意思即可。请注意,这与模式匹配无关。模式匹配与编写 Haskell 函数有关,这些函数通过构造函数检查数据,例如

null :: [a] -> Bool
null []    = True
null (_:_) = False

In your question, you confuse two separate things: GHCi commands, which start with a colon and allow you to interact with GHCi as it's running; and Haskell functions, which have ordinary names and which you can run within GHCi (but which can't, generally speaking, query it). Any Haskell tutorial will help you with ordinary functions; for GHCi commands, the best reference is probably the GHC manual's section on GHCi; there's even a subsection just about commands.

I think that Heatsink's suggestion is the best for recovering the use of :t; GHCi tries to help you out by figuring out what the command's a prefix of and running that instead, so if it guesses wrong, you'll just have to tell it what you mean. Note that this has nothing to do with pattern matching. Pattern matching has to do with writing Haskell functions which inspect data via constructors, such as

null :: [a] -> Bool
null []    = True
null (_:_) = False
行至春深 2024-10-10 21:43:44

来自“Learn You a Haskell”的起始章节。

“通过在提示符下键入 ghci 来调用交互模式。如果您在名为 myfunctions.hs 的文件中定义了一些函数,则可以通过键入 :l myfunctions 来加载这些函数,然后就可以使用它们,假设 myfunctions.hs 位于调用 ghci 的同一文件夹中,如果更改 .hs 脚本,只需再次运行 :l myfunctions 或执行 :r,这是等效的,因为它会重新加载当前脚本。”

我有点困惑你为什么要这么做?你能解释一下吗?输入额外的 .hs 并不需要花费太多精力。

test.hs 

这样做的好处还在于提醒您正在加载文件。

From "Learn You a Haskell" 's starting out chapter.

"The interactive mode is invoked by typing in ghci at your prompt. If you have defined some functions in a file called, say, myfunctions.hs, you load up those functions by typing in :l myfunctions and then you can play with them, provided myfunctions.hs is in the same folder from which ghci was invoked. If you change the .hs script, just run :l myfunctions again or do :r, which is equivalent because it reloads the current script."

I'm kind of confused as to why you did what you did in the first place? Can you explain? It's not that much more effort to type an extra .hs

test.hs 

Doing it this way also has the benefit of reminding you that you loading a file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文