6

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?

5 Answers 5

4

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

cd /Users/me
ls

ls lists the contents of the current directory; you should see your file. If you do, great! Type ghci 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.

Sign up to request clarification or add additional context in comments.

2 Comments

thank you, typing ls showed the problem: My file was really called baby.hs.txt! TextEdit seems to add a .txt suffix even if you overwrite the extension with .hs and Finder doesn't display the .txt just the .hs. It does seem to display .txt for normal textEdit files so I'm guessing it does something like display the first .suffix and hide any others.
@Alec: Ouch. Find a better editor. Even if you don't want to take the time to learn one of the classic workhorses (vim or emacs), there's tons of good text editors for OSX. Syntax highlighting is a must.
2

You're missing the module line. The first line of baby.hs should be

module Baby where

As far as doubleMe is concerned, you are missing declaring x as an argument to the function.

doubleMe x = x + x

Otherwise, your function doesn't know what x is.

2 Comments

The module line is good practice, but it's not necessary for this simple a case. It should work without it.
apologies the doubleMe error was a typo in the question. 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.
2

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:

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

When you get that working, then try leaving off the .hs. I'm not 100% sure under what circumstances that works.

3 Comments

You can give :load a filename (always including the trailing .hs or .lhs) or a module name (which must start with a capital letter).
@dave4420: You can omit the trailing .hs from file names. GHCi is smart enough to try a few variants.
thanks. Found the problem in the end, my file was really called baby.hs.txt, although Finder hides the .txt
0

@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

Comments

0

Try to open the text file with GHCi then type your command and it works

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.