Started with Haskell - And what did I learn

What I learned - not familair with compiling:

After compiling hello-world.hs, the result executable is hello-world. You are trying to run the source code itself instead of the compiled executable.

- hello-world.hs

main = putStrLn "hello world"

Compiling hello-world.hs:

$ ghc hello-world.hs 
[1 of 1] Compiling Main             ( hello-world.hs, hello-world.o )
Linking hello-world ...

Compiling the hello-world.hs creates a new hello-world without suffix.

$ tree
.
├── hello-world
├── hello-world.hi
├── hello-world.hs
└── hello-world.o

By that the hello-world ( without suffix. ) is the exacutable

What miss leaded me was in ghci you can do

$ ghci
GHCi, version 8.0.2: http://www.haskell.org/ghc/  :? for help

Prelude> :load hello-world.hs
[1 of 1] Compiling Main             ( hello-world.hs, interpreted )
Ok, modules loaded: Main.

*Main> :load hello-world
[1 of 1] Compiling Main             ( hello-world.hs, interpreted )
Ok, modules loaded: Main.
*Main> 

But as it say it’s interpreted the hello-world.hs

With Bash, Python where hello.sh and hello.py works without compiling,

Thanks all who helped me

Yeah. Bash and python are interpreted languages (an interpreted language is actually a wrong term, but what I mean is that the standard language implementation is an interpreter). It reads the code and executes it directly. Again it is more complex than that, there can be some Intermediate language or bytecode which can be saved to optimize repeated execution (this is the case for PHP for example).

Haskell (GHC) doesn’t execute code. It compiles it down to a binary. If you want to run a python server application you need a python interpreter on your server machine. It’s not the case for Haskell. For haskell I build my binaries on my home machine, upload them and they can be executed without having haskell installed on the server.

The same way you can’t just execute a C/C++ source file or java source file (even if java doesn’t produce binaries, but bytecode that can be executed by the Java VM).

I think GHCi doesn’t actually compile… It uses some bytecode provided by GHC, that is it does some compilation steps, but doesn’t produce native binaries at the end. People who knows the internals better may correct me.

1 Like

Thanks to bring this under my attention, this makes a lot of sense to me.

Great thanks - this helps me, regarding functional programming

What does the exe mean? .

On windows .exe is a suffix that indicates to the operating system that the file is an executable file.

Usually one does not need to specify it when running a command from the terminal.

Thanks for your answer, I am on Linux / Unix as could seen in my post. So the answers @nurmi-labs brings have not much meaning for me.

They have probably exactly the same output on Linux, as those tools are mostly the same, it’s just a difference in the name of the tool.

You probably hadn’t even realized if @nurmi-labs had left of the suffix.

You can also “run” a .hs file just like you can run a .py file.

$ echo "print(\"hello world\")" > hello.py
$ python hello.py
hello world

The program to do this is called runhaskell or runghc.

$ echo "main = putStrLn \"hello world\"" > hello.hs
$ runhaskell hello.hs
hello world

(see also: hello world in the Haskell Phrasebook)

1 Like