如何在Haskell中运行独立程序

我想将此程序作为独立程序运行:

module Main where main = putStrLn "Hello world!"

so I typed in ghci: --make -o hello Main.hs

but when I want to run it with ./hello I get a parse error for ./. I tried also ghc --make -o hello Main.hs but then I get an "Variable not in scope: ghc" error. Can somebody tell me, what I'm doing wrong?

解决:命令必须在外壳中运行,ghc和Main.hs必须在同一目录中

评论
shf939899
shf939899

Short answer: You do not compile the program in the ghci shell.

so I typed in ghci: --make -o hello Main.hs

It looks like you are running this in ghci (based on the error message Variable not in scope). ghci is a Haskell shell, but you do not write commands to compile or run a program in the Haskell shell itself. You run these in a shell like bash, sh, fish, etc.

So you open a terminal window, and then you can cd to the correct directory where the Main.hs file is located, then you can write:

ghc --make -o hello Main.hs

or for a Windows system, you can add a .exe extension:

ghc --make -o hello.exe Main.hs

and finally you can run this program, for example by writing ./hello in the shell.

点赞
评论