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.

Haskell in 5 steps

bash-4.3$ cat hello.hs
main = putStrLn "hello world"
bash-4.3$ ghc -o hello hello.hs
Loaded package environment from /home/eric/.ghc/i386-linux-8.6.5/environments/default
[1 of 1] Compiling Main             ( hello.hs, hello.o )
Linking hello ...
bash-4.3$ ls -l hello*
-rwxr-xr-x 1 eric eric 1782904 Jan 19 06:51 hello
-rw-r--r-- 1 eric eric     832 Jan 19 06:51 hello.hi
-rw-r--r-- 1 eric eric      30 Jan 19 06:50 hello.hs
-rw-r--r-- 1 eric eric    2232 Jan 19 06:51 hello.o
bash-4.3$ ./hello
hello world
bash-4.3$ 

Perhaps consult the Wiki regarding functional programming.

Great thanks - this helps me, regarding functional programming

The C language predated the Haskell language.

$ gcc.exe -o hello hello.c

$ ./hello.exe
Hello, World!
$

The -o option sets the name of the output file.

$ cat.exe hello.hs
main = putStrLn "hello world"

$ runghc.exe hello.hs
hello world

$ runhaskell.exe hello.hs
hello world

$

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.

HaskellPlatform-2014.2.0.0-i386-setup.exe (Yi blog Post)
Git-2.10.0-32-bit.exe (Git blog Post)

Unix-like tree.

bash-4.3$ tree --version | perl -lae 'print @F[1] if $. == 1'
v1.7.0
bash-4.3$ tree ~/haskell
/home/eric/haskell
`-- example
    |-- hello
    |-- hello.hi
    |-- hello.hs
    `-- hello.o

1 directory, 4 files
bash-4.3$ 

Use in the Windows console (cmd.exe) of tree.com,
the 1st (default) uses box characters, in the 2nd the
output characters are ASCII.

C:\Documents and Settings\Eric>TREE /F haskell
Mappetræ
Diskenhedens serienummer er C804-10A1
C:\DOCUMENTS AND SETTINGS\ERIC\HASKELL
└───example
        hello.exe
        hello.hi
        hello.hs
        hello.o


C:\Documents and Settings\Eric>TREE /F /A haskell
Mappetræ
Diskenhedens serienummer er C804-10A1
C:\DOCUMENTS AND SETTINGS\ERIC\HASKELL
\---example
        hello.exe
        hello.hi
        hello.hs
        hello.o


C:\Documents and Settings\Eric>EXIT