1

Heres my code:

factorial :: Integer -> Integer
factorial n = product [1..n]

main = print(factorial 50)

I don't get any errors compiling, but when i run the compiled code

runhaskell test

I get this error:

test:1:1: lexical error at character '\DEL'

What is causing this? How do I solve the problem?

UPDATES

I did a hexdump of the file:

$ hexdump -x test.hs

and got

0000000    6166    7463    726f    6169    206c    3a3a    4920    746e
0000010    6765    7265    2d20    203e    6e49    6574    6567    0a72
0000020    6166    7463    726f    6169    206c    206e    203d    7270
0000030    646f    6375    2074    315b    2e2e    5d6e    6d0a    6961
0000040    206e    203d    7270    6e69    2874    6166    7463    726f
0000050    6169    206c    3035    0029                                
0000057
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
dopatraman
  • 13,416
  • 29
  • 90
  • 154
  • 1
    Hex dump your file to see if it has a `0x7f` in it. – paxdiablo Dec 11 '14 at 03:54
  • Nope, no `0x7f`. please see edit – dopatraman Dec 11 '14 at 04:10
  • I added a 0x7f to a file with the same source code you showed, and got the exact same error message. Without the 0x7f, it worked. – jamshidh Dec 11 '14 at 04:11
  • 4
    I can't tell the difference between Haskell and a bar of soap but all the examples I can find have `runhaskell test.hs` (with the extension). Are you sure you're not picking up a binary file called just `test` somewhere? – paxdiablo Dec 11 '14 at 04:14
  • 2
    @paxdiablo- I think you have it, I just ran `runHaskell` on the compiled binary, and got the same error message. @dopatraman- runHaskell is an interpreter, it takes source code, if you have a compiled binary you can run it directly. – jamshidh Dec 11 '14 at 04:17
  • 1
    Adjusted your code, by the way. That hex dump shows there's no `-- test.hs` line at the top. – paxdiablo Dec 11 '14 at 04:17
  • wait, that is what i did. I ran `ghc test.hs` and that linked a `test` binary file. Then I did `runhaskell test` (the binary, not the `.hs` file). Is that what my mistake was? – dopatraman Dec 11 '14 at 04:18
  • maybe this is a dumb question, but where do i use the binary generated by the compiler? – dopatraman Dec 11 '14 at 04:18
  • @paxdiablo - I don't know if you can boast about not knowing Haskell anymore; not now that you've got a positive score in [haskell]. Next thing you know, another tag badge... – Christian Conkle Dec 11 '14 at 06:39

1 Answers1

10

Make sure that you're using runhaskell with the source file test.hs rather than the compiled binary test.

If you've used something like ghc to create an executable file, you can just run that directly, with something like:

./test

Be aware that test is probably not a good name for an executable since it's a built-in command on some shells, something that's burnt me before when my test executable doesn't seem to do what I wanted :-)

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • When I try running the executable directly a blank line gets printed to the console. – dopatraman Dec 11 '14 at 04:23
  • this happened to me. Usually I only have `foo.hs` in my directory, and `runghc foo` works fine. However, I found that when I do a *mixture* of `runghc` and `ghc`, the former attempts to run the compiled binary `foo` as a source file – xdavidliu Aug 15 '22 at 20:16