How to run a haskell file in interpreted mode

僤鯓⒐⒋嵵緔 提交于 2019-11-26 19:13:52

问题


I've been told you can interpret haskell files (which I assume means they will work like Ruby/Python/Perl). Can't find the command line option on ghc to do this, though. It always wants to compile my file. Took a look at ghci as well, but it always dumps me into a repl.

I'm basically wanting to just do ghc -i MyFile.hs (where -i is a made up flag that I'm pretending correllates to interpreted mode) and have it execute so that I can get quick feedback while I'm trying out ideas and learning.


回答1:


$ runhaskell MyFile.hs

Alternatively, runghc (they're the same thing). ghci MyFile.hs will also start an interactive REPL session with MyFile.hs loaded, but if you want to run a main program then runhaskell is the way to go.

It's probably a good idea to get into the habit of testing parts of your program as isolated units in GHCi rather than running the whole thing each time, but obviously for shorter scripts it's simplest and easiest just to run the entire thing.




回答2:


You can have a script like this:

#!/usr/bin/env runhaskell
main = putStrLn "hello world"

After making the file executable (ie chmod +x haskell_script), you can run it like any other shell script.




回答3:


Open the GHC interpreter by running ghci in a terminal, and then load a file typing :load example.hs. More details in this link.




回答4:


To run the code written in a file, say myfile.txt, containing simple lines of code which work in the GHC interpreter, like:

let a = 0 in a:[1,2]
let x = [1,2] in x ++ [3,4]

you can do:

ghc -e ':script myfile.txt'

Edit

On Windows, double quotes are required:

ghc -e ":script myfile.txt"

Instead, one can also open GHCi and do :script myfile.txt.



来源:https://stackoverflow.com/questions/8676331/how-to-run-a-haskell-file-in-interpreted-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!