Haskell - Introduction 03-20-2013, 02:06 PM
#1
Haskell
"a proof is a program; the formula it proves is a type for the program" (Curry-Howard correspondence)
Hello Hackcommunity, this is another part of the language introduction series by the HC Developers group. I decided to show you Haskell, because it is a pure functional programming language and as such very different from the imperative programming languages that most people use.
General information
Made by mathematicians for mathematicians--Haskell is your language if you like mathematics. It is especially but not only used by academics. Haskell is based on the lambda calculus, a purely functional language. Therefore it has no side effects. That makes it well suited for parallel programming. It provides list comprehension, pattern matching and type polymorphism.
Since: 1990
Paradigms: functional, lazy
Typing discipline: static, strong, inferred
Advantages
Resources
Documentation: hackage
Compiler: GHC
IDE: haskellplatform
E-Book: realworldhaskell
Search engines: hoogle,hayoo
First program
For your first program with Haskell we will use the GHC. You find the download link for GHC in the section above.
GHC has three components:
1. The compiler that compiles haskell source files to native code. The compiler is called ghc.
2. The interactive interpreter and debugger called ghci.
3. The interpreter, runghc, that executes haskell source files as script.
Using ghc
Now open a text editor (i.e. notepad) and write the following line:
Save the file as hello.hs.
To create an executable open the terminal and go to the directory, where you saved hello.hs.
Type the following to compile the program:
Your executable is now called hello. To run the program you just type
In Linux/Unix:
In Windows:
Your terminal should now display the string "Hello, Hackcommunity!"
Using ghci
Start ghci by typing:
The interactive compiler should start.
You can load your haskell source by typing the following command into ghci:
This loads hello.hs into the interpreter.
Now call the function main by just typing in:
And it should again print the string "Hello, Hackcommunity!" to your screen.
Type in
to leave ghci
Using runghc
runghc executes your program immediately. It doesn't compile it to native code first.
Doing this is simple:
will run your first program and once again print "Hello, Hackcommunity!" to your screen.
"a proof is a program; the formula it proves is a type for the program" (Curry-Howard correspondence)
Hello Hackcommunity, this is another part of the language introduction series by the HC Developers group. I decided to show you Haskell, because it is a pure functional programming language and as such very different from the imperative programming languages that most people use.
General information
Made by mathematicians for mathematicians--Haskell is your language if you like mathematics. It is especially but not only used by academics. Haskell is based on the lambda calculus, a purely functional language. Therefore it has no side effects. That makes it well suited for parallel programming. It provides list comprehension, pattern matching and type polymorphism.
Since: 1990
Paradigms: functional, lazy
Typing discipline: static, strong, inferred
Advantages
- no side effects
- easy to prove, an equal sign actually means equality in the mathematical sense
- well suited for mathematical problems
- well suited for parallel programming
- the language designers have no qualm to change the language, if it is better for it; for that reason Haskell hasn't aged like Java did
- no design patterns needed
- concise code (quicksort example: qsort (p:xs) = qsort [x | x<-xs, x<p] ++="" [p]="" qsort="" [x="" |="" x<-xs,="" x="">=p] )
- in order to understand the underlying concepts of this language you need some knowledge in mathematics
- backward compatibility is not ensured; old programs might not run anymore at some point
Resources
Documentation: hackage
Compiler: GHC
IDE: haskellplatform
E-Book: realworldhaskell
Search engines: hoogle,hayoo
First program
For your first program with Haskell we will use the GHC. You find the download link for GHC in the section above.
GHC has three components:
1. The compiler that compiles haskell source files to native code. The compiler is called ghc.
2. The interactive interpreter and debugger called ghci.
3. The interpreter, runghc, that executes haskell source files as script.
Using ghc
Now open a text editor (i.e. notepad) and write the following line:
Code:
main = putStrLn "Hello, Hackcommunity!"Save the file as hello.hs.
To create an executable open the terminal and go to the directory, where you saved hello.hs.
Type the following to compile the program:
Code:
ghc -o hello hello.hsYour executable is now called hello. To run the program you just type
In Linux/Unix:
Code:
./helloIn Windows:
Code:
hello.exeYour terminal should now display the string "Hello, Hackcommunity!"
Using ghci
Start ghci by typing:
Code:
ghciThe interactive compiler should start.
You can load your haskell source by typing the following command into ghci:
Code:
:l hello.hsThis loads hello.hs into the interpreter.
Now call the function main by just typing in:
Code:
mainAnd it should again print the string "Hello, Hackcommunity!" to your screen.
Type in
Code:
:qto leave ghci
Using runghc
runghc executes your program immediately. It doesn't compile it to native code first.
Doing this is simple:
Code:
runghc hello.hswill run your first program and once again print "Hello, Hackcommunity!" to your screen.
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.
Expressed feelings are just an attempt to simulate humans.
![[Image: 2YpkRjy.png]](http://i.imgur.com/2YpkRjy.png)

![[+]](https://sinister.li/images/modern/collapse_collapsed.png)
![[Image: OilyCostlyEwe.gif]](http://fat.gfycat.com/OilyCostlyEwe.gif)