简体   繁体   English

从GHCi中的文件加载函数时出错

[英]Error loading function from file in GHCi

I'm completely new to Haskell. 我对Haskell完全不熟悉。 To grasp the basics I've started working through 'Learn you a Haskell for Great Good'. 为了掌握基础知识,我已经开始研究“了解一个好的Haskell”。 I'm stuck on the simple matter of loading a function from a file. 我坚持从文件加载函数的简单问题。

The file is called baby.hs and contains the function 该文件名为baby.hs并包含该函数

doubleMe x = x + x

and nothing else. 没有别的。 I've saved it in /Users/me . 我把它保存在/Users/me

Typing :load baby into GHCi, I get the following error: 打字:load baby到GHCi中,我收到以下错误:

target `baby' is not a module name or a source file. target“baby”不是模块名称或源文件。

I'm working on a Mac and I created my baby.hs file using TextEdit set to produce a plain text/UTF-8 file. 我正在使用Mac,我使用TextEdit设置创建了我的baby.hs文件,以生成纯文本/ UTF-8文件。 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. 我认为我的主目录是/Users/me虽然我不知道如何从GHCi检查这个,但是从我在运行GHCi之前从bash检查时。

Any idea what I'm doing wrong? 知道我做错了什么吗?

As @clintm suggests, first fix your doubleMe function. 正如@clintm建议的那样,首先修复你的doubleMe函数。 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 让ghci找到你的文件的最简单方法是确保你从保存文件的同一目录启动ghci。打开一个终端窗口,然后输入

cd /Users/me
ls

ls lists the contents of the current directory; ls列出当前目录的内容; you should see your file. 你应该看到你的文件。 If you do, great! 如果你这样做,太好了! Type ghci at the bash prompt, and :load baby should work. 在bash提示符下键入ghci ,并且:load baby应该可以正常工作。 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. 返回TextEdit或使用Spotlight查看您真正放置它的位置。

You're missing the module line. 你错过了模块行。 The first line of baby.hs should be baby.hs的第一行应该是

module Baby where

As far as doubleMe is concerned, you are missing declaring x as an argument to the function. doubleMe而言,您缺少将x声明为函数的参数。

doubleMe x = x + x

Otherwise, your function doesn't know what x is. 否则,您的函数不知道x是什么。

Try using the complete path, for example: 尝试使用完整路径,例如:

:load /Users/me/baby.hs

You should also be able to use relative paths. 您还应该能够使用相对路径。 Try navigating to the directory that baby.hs is in first: 尝试导航到baby.hs首先出现的目录:

% cd /Users/me
% ghci
GHCi blah blah blah
Prelude> :load baby.hs

When you get that working, then try leaving off the .hs . 当你开始工作时,试着离开.hs I'm not 100% sure under what circumstances that works. 我不是100%肯定在什么情况下有效。

@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." @Alec:“问题是这个文件真的叫做baby.hs.txt,但我没有发现,因为Finder隐藏了.txt部分。”

You can work around this in TextEdit... 你可以在TextEdit中解决这个问题...

  • select your baby.hs.txt file 选择你的baby.hs.txt文件

  • 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 在Name&Extension区域输入baby.hs

  • close the Info dialog 关闭“信息”对话框

  • another dialog asks if you really want the .hs extension 另一个对话框询问你是否真的想要.hs扩展名

  • confirm that you do and you're good-to-go 确认你这样做,而且你很开心

尝试使用GHCi打开文本文件,然后键入您的命令,它的工作原理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM