Compiling Haskell code with Cygwin - haskell

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.

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.

Running a haskell program in ghci

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?

cygwin does not create a.out file when I compile for ocaml

I'm currently trying to start learning ocaml with cygwin, but when I try to compile an ml file, nothing happens - no error messages and no files created.
The command I used on cygwin was,
$ ocamlc hello.ml
According to my Prof., there should be a file called, a.out created on my working folder, but there was nothing.
But when I tried with this command,
$ ocamlc -o a.out hello.ml
a.out file was created properly, and I could run the compiled program using
$ ./a.out
as pointed out by the Prof.
For some experiment, I typed in the following,
$ ocamlc -o a hello.ml
This created a file named 'a' without any file extension.
So my question is,
1. Why doesn't it create the a.out file in the first place? - it should have been done according to the Prof.
2. What does the .out file do?
Any new file created when you type ocamlc hello.ml? Maybe camlprog.exe around? If it is, you are using MinGW OCaml over Cygwin. MinGW OCaml is a windows app therefore its default executable file name is not a.out, which is the default name for Unix and Cygwin.
I never recommend ppl to learn OCaml with Windows or Cygwin. Since there are 3 flavors: Cygwin OCaml, MinGW OCaml and MSVC OCaml and they behave slightly different like this. And newcomers are never sure which flavor they are actually using.

How to do i18n and create Windows Installer of Haskell programs?

I'm considering using Haskell to develop for a little commercial project. The program must be internationalized (to Simplified Chinese, to be specific), and my customer requests that it should be delivered in a one-click Windows Installer form. So basically these are the two problems I'm facing now:
I18n of Haskell programs: the method described in Internationalization of Haskell programs did work (partially) if I change the command of executing the program from LOCALE=zh_CN.UTF-8 ./Main to LANG=zh_CN.UTF-8 ./Main (I'm working on Ubuntu 10.10), however, the Chinese output is garbled, and I've no idea why is that.
Distribution on Windows: I'm used to work under Linux and build & package my Haskell programs using Cabal, but what is the most natural way to create a one-click Windows Installer from a cabalized Haskell package? Is the package bamse the right way to go?
------ Details for the first problem ------
What I did was:
$ hgettext -k __ -o messages.pot Main.hs
$ msginit --input=messages.pot --locale=zh_CN.UTF-8
(Edit the zh_CN.po file, adding Chinese translation)
$ mkdir -p zh_CN/LC_MESSAGES
$ msgfmt --output-file=zh_CN/LC_MESSAGES/hello.mo zh_CN.po
$ ghc --make Main.hs
$ LANG=zh_CN.UTF-8 ./Main
And the output was like:
This indicates gettext is actually working, but for some reason the generated zh_CN.mo file is broken (my guess). I'm pretty sure my zh_CN.po file is encoded in UTF-8. Plus, aside from using System.IO.putStrLn, I also tried System.IO.UTF8.putStrLn to output the string, which didn't work either.
For the first question, the solution is to add decodeString from Codec.Binary.UTF8.String to the gettext function __, like this:
__ :: String -> String
__ = decodeString . unsafePerformIO . getText
And then everything works just fine.
for the first question, is it possible that you didn't install correct font or the font is not set to be used in the terminal.

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