This draft deletes the entire topic.
Examples
-
Online REPL
The easiest way to get started writing Haskell is probably by going to the Haskell website or Try Haskell and use the online REPL (read-eval-play-loop) on the home page. The online REPL supports most basic functionality and even some IO. There is also a basic tutorial available which can be started by typing the command
help
. An ideal tool to start learning the basics of Haskell and try out some stuff.GHC(i)
For programmers that are ready to engage a little bit more, there is GHCi, an interactive environment that comes with the Glorious/Glasgow Haskell Compiler. The GHC can be installed separately, but that is only a compiler. In order to be able to install new libraries, tools like Cabal and Stack must be installed as well. It is probably easier to install a Haskell Platform. The platform exists in two flavours:
- The minimal distribution contains only GHC (to compile) and Cabal/Stack (to install and build packages)
- The full distribution additionally contains tools for project development, profiling and coverage analysis. Also an additional set of widely-used packages is included.
These platforms can be installed by downloading an installer and following the instructions or by using your distribution's package manager (note that this version is not guaranteed to be up-to-date):
-
Ubuntu, Debian, Mint:
sudo apt-get install haskell-platform
-
Fedora:
sudo dnf install haskell-platform
-
Redhat:
sudo yum install haskell-platform
-
Gentoo:
sudo layman -a haskell sudo emerge haskell-platform
-
OSX with Homebrew:
brew cask install haskell-platform
-
OSX with MacPorts:
sudo port install haskell-platform
Once installed, it should be possible to start GHCi by invoking the
ghci
command anywhere in the terminal. If the installation went well, the console should look something likeme@notebook:~$ ghci GHCi, version 6.12.1: http://www.haskell.org/ghc/ :? for help Prelude>
possibly with some more information on what libraries have been loaded before the
Prelude>
. Now, the console has become a Haskell REPL and you can execute Haskell code as with the online REPL. In order to quit this interactive environment, one can type:q
or:quit
. For more information on what commands are available in GHCi, type:?
as indicated in the starting screen.Because writing the same things again and again on a single line is not always that practically, it might be a good idea to write the Haskell code in files. These files normally have
.hs
for an extension and can be loaded into the REPL by using:l
or:load
.As mentioned earlier, GHCi is a part of the GHC, which is actually a compiler. This compiler can be used to transform a
.hs
file with Haskell code into a running program. Because a.hs
file can contain a lot of functions, amain
function must be defined in the file. This will be the starting point for the program. The filetest.hs
can be compiled with the commandghc test.hs
this will create object files and an executable if there were no errors and the
main
function was defined correctly.More advanced tools
-
It has already been mentioned earlier as package manager, but stack can be a useful tool for Haskell development in completely different ways. Once installed, it is capable of
- installing (multiple versions of) GHC
- project creation and scaffolding
- dependency management
- building and testing projects
- benchmarking
-
IHaskell is a haskell kernel for IPython and allows to combine (runnable) code with markdown and mathematical notation. For OS X users, there is even an application called Kronos Haskell
-
A basic "Hello, World!" program in Haskell can be expressed concisely in just two lines:
main :: IO () main = putStrLn "Hello, World!"
The first line is an optional type annotation, indicating that
main
is an I/O action of typeIO ()
. This meansmain
yields something of type()
(the empty tuple, containing no information)Put this into a
helloworld.hs
file and compile it using a Haskell compiler, such as GHC:ghc -o helloworld helloworld.hs
Use
runhaskell
orrunghc
to run the program in interpreted mode:runhaskell helloworld.hs
The interactive REPL can also be used instead of compiling. It comes shipped with most Haskell environments, such as
ghci
:ghci> putStrLn "Hello World!"
Alternatively, load scripts into ghci from a file using
load
(or:l
). This saves all scripts for future review:ghci> :load helloworld
:reload
(or:r
) reloads everything in ghci:Prelude> :load helloworld.hs [1 of 1] Compiling Main ( helloworld.hs, interpreted ) <some time later after some edits> *Main> :r Ok, modules loaded: Main.
Explanation:
This first line is a type signature, declaring the type of
main
:main :: IO ()
IO ()
type objects often describe actions which write to the outside world.Because Haskell has a fully-fledged Hindley-Milner type system, type signatures are technically optional: if you simply omit the
main :: IO ()
, the compiler will be able to infer this information itself by looking at the definition ofmain
. However, it is very much considered bad style not to write type signatures for top-level definitions. The reasons include:- Type signatures in Haskell are a very helpful piece of documentation because the type system is so expressive that you often can see what sort of thing a function is good for simply by looking at its type. This “documentation” can be conveniently accessed with tools like GHCi. And unlike normal documentation, the compiler's type checker will make sure it actually matches the function definition!
- Type signatures keep bugs local. If you make a mistake in the definition of
main
, the compiler may not immediately report an error, but instead simply infer a nonsensical type formain
, with which it actually typechecks. You may then get a cryptic error message when using it.
With a signature, the compiler is very good at spotting bugs right where they happen.
This second line does the actual work:
main = putStrLn "Hello, World!"
If you come from an imperative language, it may be helpful to note that the definition can also be written as:
main = do { putStrLn "Hello, World!"; return () }
Or preferably (Haskell has layout-based scoping):
main = do putStrLn "Hello, World!" return ()
In actuality, the
do
syntax is just syntactic sugar for sequencing monads likeIO
, andreturn ()
is a no-op action. It is completely equivalent to just definingmain = putStrLn "Hello, World!"
. The “statement”putStrLn "Hello, World!"
can be seen as a complete program, and you simply definemain
to refer to this program.To see why, you can look up the signature of
putStrLn
:putStrLn :: String -> IO ()
This is a function that takes a string as its argument and outputs an IO-action (i.e. a program that the runtime can execute). What the runtime will actually execute is always the
main
action, so we simply need to define it as equal toputStrLn "Hello, World!"
. -
-
The factorial function is a Haskell "Hello World!" (and for functional programming generally) in the sense that it succinctly demonstrates basic principles of the language.
Variation 1
fac :: (Integral a) => a -> a fac n = product [1..n]
Integral
is the class of integral number types. Examples includeInt
andInteger
.(Integral a) =>
places a constraint on the typea
to be in said classfac :: a -> a
says thatfac
is a function that takes ana
and returns ana
product
is a function that accumulates all numbers in a list by multiplying them together.[1..n]
is special notation which desugars toenumFromTo 1 n
, and is the range of numbers1 ≤ x ≤ n
.
Variation 2
fac :: (Integral a) => a -> a fac 0 = 1 fac n = n * fac (n - 1)
This variation uses pattern matching to split the function definition into separate cases. The first definition is invoked if the argument is
0
(sometimes called the stop condition) and the second definition otherwise (the order of definitions is significant). It also exemplifies recursion asfac
refers to itself.
It is worth noting that, due to rewrite rules, both versions of
fac
will compile to identical machine code when using GHC with optimizations activated. So, in terms of efficiency, the two would be equivalent. -
Lazy evaluation means Haskell will evaluate only list items whose values are needed.
The basic recursive definition is:
f (0) <- 0 f (1) <- 1 f (n) <- f (n-1) + f (n-2)
If evaluated directly, it will be very slow. But, imagine we have a list that records all the results,
fibs !! n <- f (n)
Then
┌──────┐ ┌──────┐ ┌──────┐ │ f(0) │ │ f(1) │ │ f(2) │ fibs -> 0 : 1 : │ + │ : │ + │ : │ + │ : ..... │ f(1) │ │ f(2) │ │ f(3) │ └──────┘ └──────┘ └──────┘ ┌────────────────────────────────────────┐ │ f(0) : f(1) : f(2) : ..... │ └────────────────────────────────────────┘ -> 0 : 1 : + ┌────────────────────────────────────────┐ │ f(1) : f(2) : f(3) : ..... │ └────────────────────────────────────────┘
This is coded as:
GHCi> let fibs = 0 : 1 : zipWith (+) fibs (tail fibs) GHCi> take 10 fibs [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
zipWith
makes a list by applying a given binary function to corresponding elements of the two lists given to it, sozipWith (+) [x1, x2, ...] [y1, y2, ...]
is equal to[x1 + y1, x2 + y2, ...]
.Another way of writing
fibs
is with thescanl
function:GHCi> let fibs = 0 : scanl (+) 1 fibs GHCi> take 10 fibs [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
scanl
builds the list of partial results thatfoldl
would produce, working from left to right along the input list. That is,scanl f z0 [x1, x2, ...]
is equal to[z0, z1, z2, ...] where z1 = f z0 x1; z2 = f z1 x2; ...
.Thanks to lazy evaluation, both functions define infinite lists without computing them out entirely. That is, we can write a
fib
function, retrieving the nth element of the unbounded Fibonacci sequence:GHCi> let fib n = fibs !! n -- (!!) being the list subscript operator -- or in point-free style: GHCi> let fib = (fibs !!) GHCi> fib 9 34
-
We can declare a series of expressions in the REPL like this:
Prelude> let x = 5 Prelude> let y = 2 * 5 + x Prelude> let result = y * 10 Prelude> x 5 Prelude> y 15 Prelude> result 150
To declare the same values in a file we write the following:
-- demo.hs module Demo where -- We declare the name of our module so -- it can be imported by name in a project. x = 5 y = 2 * 5 + x result = y * 10
Module names are capitalized, unlike variable names.
-
Just the few most salient variants:
Below 100
import Data.List (\\) ps100 = ((([2..100] \\ [4,6..100]) \\ [6,9..100]) \\ [10,15..100]) \\ [14,21..100] -- = (((2:[3,5..100]) \\ [9,15..100]) \\ [25,35..100]) \\ [49,63..100] -- = (2:[3,5..100]) \\ ([9,15..100] ++ [25,35..100] ++ [49,63..100])
Unlimited
Using data-ordlist package:
import Data.List.Ordered (minus, unionAll) ps = 2 : _Y ((3:) . minus [5,7..] . unionAll . map (\p -> [p*p, p*p+2*p..])) _Y g = g (_Y g)
Traditional
(a sub-optimal trial division algorithm)
sieve (x:xs) = x : sieve [y | y <- xs, rem y x > 0] ps = sieve [2..] -- = map head ( iterate (\(x:xs) -> filter ((> 0).(`rem`x)) xs) [2..] )
Optimal trial division
ps = 2 : [n | n <- [3..], all ((> 0).rem n) $ takeWhile ((<= n).(^2)) ps] -- = 2 : [n | n <- [3..], foldr (\p r-> p*p > n || (rem n p > 0 && r)) True ps]
Transitional
from trial division to sieve of Eratosthenes:
[n | n <- [2..], []==[i | i <- [2..n-1], j <- [i*i, i*i+i..n], j==n]]
The Shortest Code
nubBy (((>1).).gcd) [2..] -- i.e., nubBy (\a b -> gcd a b > 1) [2..]
nubBy
is fromData.List
.
Remarks
Haskell is an advanced purely-functional programming language.
Features:
- Statically typed: Every expression in Haskell has a type which is determined at compile time. Static type checking is the process of verifying the type safety of a program based on analysis of a program's text (source code). If a program passes a static type checker, then the program is guaranteed to satisfy some set of type safety properties for all possible inputs.
- Purely functional: Every function in Haskell is a function in the mathematical sense. There are no statements or instructions, only expressions which cannot mutate variables (local or global) nor access state like time or random numbers.
- Concurrent: Its flagship compiler, GHC, comes with a high-performance parallel garbage collector and light-weight concurrency library containing a number of useful concurrency primitives and abstractions.
- Lazy evaluation: Functions don't evaluate their arguments. Delays the evaluation of an expression until its value is needed.
- General-purpose: Haskell is built to be used in all contexts and environments.
- Packages: Open source contribution to Haskell is very active with a wide range of packages available on the public package servers.
The latest standard of Haskell is Haskell 2010. As of May 2016, a group is working on the next version, Haskell 2020.
The official Haskell documentation is also a comprehensive and useful resource. Great place to find books, courses, tutorials, manuals, guides, etc.
Topic Outline
Versions
Sign up or log in
Save edit as a guest
Join Stack Overflow
Using Google
Using Facebook
Using Email and Password
We recognize you from another Stack Exchange Network site!
Join and Save Draft