从 GHCi 中的文件加载函数时出错
我对 Haskell 完全陌生。为了掌握基础知识,我开始学习“为伟大的利益而学习 Haskell”。我陷入了从文件加载函数的简单问题。
该文件名为 baby.hs
,包含该函数
doubleMe x = x + x
,仅包含该函数。我已将其保存在 /Users/me
中。
在 GHCi 中输入 :load Baby
时,出现以下错误:
目标“baby”不是模块名称或源文件。
我在 Mac 上工作,并使用 TextEdit 创建了我的 baby.hs
文件,以生成纯文本/UTF-8 文件。我认为我的主目录是 /Users/me
虽然我不确定如何从 GHCi 中检查它,但它来自我在运行 GHCi 之前从 bash 检查时的情况。
知道我做错了什么吗?
I'm completely new to Haskell. To grasp the basics I've started working through 'Learn you a Haskell for Great Good'. I'm stuck on the simple matter of loading a function from a file.
The file is called baby.hs
and contains the function
doubleMe x = x + x
and nothing else. I've saved it in /Users/me
.
Typing :load baby
into GHCi, I get the following error:
target `baby' is not a module name or a source file.
I'm working on a Mac and I created my baby.hs
file using TextEdit set to produce a plain text/UTF-8 file. I think my home directory is /Users/me
although I'm not sure how to check this from GHCi, it is from when I check from bash before running GHCi.
Any idea what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正如 @clintm 建议的那样,首先修复你的 doubleMe 函数。你所拥有的将会给出错误——但不会是你报告的错误。
让 ghci 找到您的文件的最简单方法是确保从保存文件的同一目录启动 ghci。打开一个终端窗口,然后输入
ls 列出当前目录的内容;你应该看到你的文件。如果你这样做,那就太好了!在 bash 提示符下输入 ghci,然后
:load Baby
应该可以工作。如果没有,则说明您尚未将文件保存在您认为已保存的位置。返回到 TextEdit 或使用 Spotlight 查看您实际放置的位置。As @clintm suggests, first fix your doubleMe function. What you have will give errors --- but not the errors you're reporting.
The simplest way to get ghci to find your file is to make sure you start ghci from the same directory your file is saved in. Open a terminal window, and type
ls
lists the contents of the current directory; you should see your file. If you do, great! Typeghci
at the bash prompt, and:load baby
should work. If not, you haven't saved your file where you think you have. Go back to TextEdit or use Spotlight to see where you've really put it.您缺少模块行。 Baby.hs 的第一行应该是
就
doubleMe
而言,您缺少将x
声明为函数的参数。否则,您的函数不知道
x
是什么。You're missing the module line. The first line of baby.hs should be
As far as
doubleMe
is concerned, you are missing declaringx
as an argument to the function.Otherwise, your function doesn't know what
x
is.尝试使用完整路径,例如:
您还应该能够使用相对路径。首先尝试导航到
baby.hs
所在的目录:当您开始工作时,然后尝试忽略
.hs
。我不能 100% 确定在什么情况下它会起作用。Try using the complete path, for example:
You should also be able to use relative paths. Try navigating to the directory that
baby.hs
is in first:When you get that working, then try leaving off the
.hs
. I'm not 100% sure under what circumstances that works.@Alec:“问题是该文件实际上名为baby.hs.txt,但我没有发现这一点,因为Finder 由于某种原因隐藏了.txt 部分。”
您可以在 TextEdit 中解决此问题...
选择您的baby.hs.txt 文件
用两根手指点击它以弹出上下文菜单
选择获取信息以打开文件的信息对话框
在名称和名称中输入baby.hs扩展区
关闭“信息”对话框
另一个对话框询问您是否确实需要 .hs 扩展名
确认您已这样做并且可以开始
@Alec: "The problem was that the file was really called baby.hs.txt but I didn't spot that as Finder hide the .txt part for some reason."
You can work around this in TextEdit...
select your baby.hs.txt file
two-finger tap it to pop up the context menu
select Get Info to open the file's Info dialog
enter baby.hs in the Name & Extension area
close the Info dialog
another dialog asks if you really want the .hs extension
confirm that you do and you're good-to-go
尝试使用 GHCi 打开文本文件,然后输入命令,它就可以工作
Try to open the text file with GHCi then type your command and it works