简体   繁体   中英

Opening a .hs file with ghci in Terminal?

I'm using Mountain Lion. I open the terminal, then I load ghci , I write :l and then I try to load my file (which is in my desktop) by dragging it with the mouse from my desktop to the terminal, so I know that the location is correct and I get this, thank you in advance:

Prelude> :l  /Users/myusername/Desktop/Test.hs 
[1 of 1] Compiling Main             ( /Users/myusername/Desktop/Test.hs, interpreted )

/Users/myusername/Desktop/Test.hs:1:7: parse error on input `\'
Failed, modules loaded: none.
Prelude> 

Edit: The file Im trying to open (written in Text Edit) is:

double :: Int -> Int
double x = x + x

TextEdit is not a plaintext editor (unlike eg Windows Notepad), so by default it will include formatting junk in your files that GHC obviously isn't happy about. Apparently you can still use TextEdit if set up correctly , but it's quite recommendable to use a proper programming editor. Like any Unix, OSX comes with a vi flavour, which takes some time to get used to but isn't that hard and works fine; at least you can use it to check what's really in your file.

vi /Users/myusername/Desktop/Test.hs

or, even simpler

cat /Users/myusername/Desktop/Test.hs

will just give you the exact contents of your file.

For the choice what editor to use best, consider this question .

I tried the same procedure on Windows and it worked perfectly.

Have you tried to go to the directory inside GHCi and open it?

The procedure would be:

Prelude> :cd /Users/myusername/Desktop/
Prelude> :l Test.hs

For me, copying/pasting the code you have posted, both situations worked on Windows.

The presence of a \\ implies you've got an RTF (rich text) file from TextEdit. RTF is TextEdit's default, and it's a format that annotates plain text with information about text font, size, etc.

I'd recommend vi or emacs, but to fix your immediate problem, open the file in TextEdit and hit Cmd-Shift-t to convert your file to plain text.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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