Running a haskell program in ghci - haskell

I am new to Haskell and trying to learn it from "Learn you a Haskell." I have run into a problem that I cannot find an answer to anywhere. I have a simple program that I want to run, but nothing will I do will make it run. What the book is telling me to do doesn't work. I can compile the program and run individual functions, but I can't get main to run unless I call that particular function. That seemed fine to me until I tried to pass a text file into it and it doesn't work.
So what do I do to run the program after typing :load program.hs?
I have tried...
$ ./program
--make program
--make program.exe
and about a thousand variations of these things. What the hell do I do to get my program running so that I can pass it a text file?
Picture of results in GHCi
cmd "Assembler failure"

It looks like you got confused between ghci and the command line. You can only type Haskell code in ghci. The command ./capslocker < haiku.txt is meant to be run from the command line and will run your compiled program capslocker. The $ sign is the command prompt in Linux and you're not meant to type that in. The book suggests using
$ ghc --make capslocker
beforehand to compile the code. It doesn't actually use ghci in this section. If you're on Windows then some of the commands may not work, since it assumes you are using Linux (it explains this earlier in the "input and output" section and suggests cygwin as an alternative).

Haskell can be compiled or interpreted. To use a python-like interpreter do runhaskell and you can use the same parameters as you would compile it.
More information here:
What's the difference between runghc and runhaskell?

Related

Compiling simple programs on Haskell on Windows using the command line

This is a follow-up to this question, which is about whether it is possible to compile simple programs on Haskell in Windows, without recourse to Cygwin: Compiling Haskell programs in Windows: is it possible without downloading something such as Cygwin?
For background, I asked this question, since if there were some other way of compiling the program it would be very useful to know, since I am on a university computer and cannot download things like Cygwin without permission. (and even with permission it might not be possible, depending on what Cygwin requires)
Someone responded to my question, suggesting I open the command line and put ghc --make helloworld and hit Enter. However, when I put in ghc --make helloworld and hit Enter this comes up:
ghc: unrecognised flag: --
did you mean one of:
-D
-F
-H
Usage: For basic infomration, try the '--help' option
The person answering the question suggested I made another question, asking why I received the above message. How can I deal with this problem?
Yes, it is possible to use Windows to compile Haskell programs. In fact, I use Windows for all my Haskell programming! To compile a Haskell program, use ghc --make <program>; for instance, here it would be ghc --make helloworld.hs. Note that there is no space between -- and make; including this space gives the error you describe. After running this command, an executable helloworld.exe file is produced.

Compiling Haskell code with Cygwin

I am reading "Learn you a Haskell for great good", and have reached the chapter on I/O actions. I am using Windows, and have downloaded GHCi and WinGHCi.
I'm trying to compile a simple program and have saved a file on emacs as helloworld.hs with main = putStrLn "hello, world" in it.
The book says
Open your terminal, navigate to the directory where helloworld.hs is located, and enter the following:
$ ghc --make helloworld
I am using Cygwin.
If I put $ ghc --make helloworld I receive target 'helloworld' is not a module name or a source file. Do I have to write some specific commands in Cygwin prior to writing $ ghc --make helloworld?
How can I navigate to where helloworld.hs is, or enter the full path to the file? It is in the Local disk (C:), emacs-25.3_1-x86_64 , bin
From the screen shot, your current working directory is /cygdrive/c, and as the ls command shows, there's no file there called helloworld.hs.
I think you'll need to either navigate to where helloworld.hs is, or perhaps, alternatively, you may be able to enter the full path to the file.
You can use the cd command to change directory.

Make hlint run ever time ghc and ghci does?

How can you make HLint run evertime you compile or interpret a haskell module with ghc (or inside of ghci.)
Note: I am using geany on Ubuntu.

How to load example in Djinn/UU/Examples/Equality.hs?

After cabal install Djinn which is using QuickCheck, the executable file is in ./.cabal/bin/djinn.
then I copy the executable to the directory Downloads/Djinn/UU/Examples/
want to run example Equality.hs then djinn Equality.hs, can not parse command
then ./djinn then :load full path/UU/Examples/Equality.hs
return cannot parse command
As the comment says, this doesn't make much sense. Djinn doesn't work on Haskell source files as far as I know. Also Djinn doesn't appear to use quickcheck.

how to execute haskell program in cygwin

I compiled my helloworld.hs and got a helloworld.o file, I tried ./helloworld, but it didn't work, so what is the right way to execute the helloworld?
I am using cygwin, I just write down $ ghc --make helloworld.hs and I get helloworld.hi, helloworld.exe.manifest, helloworld.o files, I don't know what do I need to do next...
Depending on whether you used a Cygwin ghc or a Windows native ghc, you got either a.out (a historical traditional name) or helloworld.exe. If you have a.out you'll need to rename it to something.exe to execute it on Windows.
You can easily tell ghc how to call the executable: ghc -o helloworld.exe --make helloworld.hs.
By the way ghc --help would have told you:
To compile and link a complete Haskell program, run the compiler like so:
    ghc-6.8.2 --make Main
where the module Main is in a file named Main.hs (or Main.lhs) in the current directory. The other modules in the program will be located and compiled automatically, and the linked program will be placed in the file a.out' (orMain.exe' on Windows).
As you haven't specified anything about how you compiled, such as for instance what compiler you're using, we can only guess.
The common way to get a .o (object) file out of ghc is using the -c switch; as the manual says, that means "do not link". The mnemonic is "compile only". Without linking, you have only a portion of a program, and it cannot be executed. Precisely what it needs to be linked against will depend on the particular object file, and some of that is filled in by default if you simply let the compiler run the linker. Linking separately is more complicated.

Resources