Search This Blog

Tuesday, January 15, 2013

How to start Haskell on ubuntu

Haskell in 5 steps

Haskell is a general purpose, purely functional programming language. This page will help you get started as quickly as possible.

Contents

[hide]


1 Install Haskell

The recommended way to get started with programming Haskell is the Haskell Platform. The Platform comes with GHC, the de-facto standard Haskell compiler, with many useful tools that will let you program Haskell painlessly. The installation should be easy and simple enough on most operating systems.
Try Haskell provides a less complete but quicker way to give Haskell a shot.

2 Start Haskell

If you have installed the Haskell Platform, open a terminal and type ghci (the name of the executable of the GHC interpreter) at the command prompt. Alternatively, if you are on Windows, you may choose WinGHCi in the Start menu.
    $ ghci
    GHCi, version 6.12.3: http://www.haskell.org/ghc/  :? for help
    Loading package base ... linking ... done.
    Prelude>
And you are presented with a prompt. The Haskell system now attentively awaits your input.

3 Write your first Haskell program

If you've learned to program another language, your first program probably was "Hello, world!", so let's do that:
Prelude> "Hello, World!"
"Hello, World!"
The Haskell system evaluated the string, and printed the result. Or we can try a variation to print directly to standard output:
Prelude> putStrLn "Hello World"
Hello World
Using a Haskell compiler, such as GHC, you can compile the code to a standalone executable. Create a source file hello.hs containing:
main = putStrLn "Hello, World!"
And compile it with:
    $ ghc -o hello hello.hs
You can then run the executable (./hello on Unix systems, hello.exe on Windows):
    $ ./hello
    Hello, World!

4 Haskell the calculator

Let's do something fun. In Haskell, your first true program is the factorial function. So back to the interpreter now and let's define it:
Prelude> let fac n = if n == 0 then 1 else n * fac (n-1)
This defines a new function called fac which computes the factorial of an integer.
We can now run fac on some argument:
Prelude> fac 42
1405006117752879898543142606244511569936384000000000
Congratulations! Programming made easy. Note that if you're using Hugs, you'll need to load the definition of fac from a file, fac.hs, containing:
fac n = if n == 0 then 1 else n * fac (n-1)
And run it with Hugs as follows (this also works in GHCi):
Hugs.Base> :load fac.hs
Main> fac 42
1405006117752879898543142606244511569936384000000000
We can of course compile this program, to produce a standalone executable. In the file fac.hs we can write (and let's use elegant pattern matching syntax just for fun):
fac 0 = 1
fac n = n * fac (n-1)
 
main = print (fac 42)
which can then be compiled and run:
    $ ghc -o fac fac.hs
    $ ./fac
    1405006117752879898543142606244511569936384000000000
Great!

4.1 Write your first parallel Haskell program

Haskell has good support for parallel and multicore programming. We can write a parallel program by adding `par` to expressions, like so:
import Control.Parallel
 
main = a `par` b `par` c `pseq` print (a + b + c)
    where
        a = ack 3 10
        b = fac 42
        c = fib 34
 
fac 0 = 1
fac n = n * fac (n-1)
 
ack 0 n = n+1
ack m 0 = ack (m-1) 1
ack m n = ack (m-1) (ack m (n-1))
 
fib 0 = 0
fib 1 = 1
fib n = fib (n-1) + fib (n-2)
Compiling with -threaded and optimizations on:
$ ghc -O2 --make A.hs -threaded -rtsopts
[1 of 1] Compiling Main             ( A.hs, A.o )
Linking A ...
Note that in versions of GHC prior to GHC 7, you can leave off the -rtsopts flag
And now we can run our multicore program. Here across two cores:
$ time ./A +RTS -N2
1405006117752879898543142606244511569936384005711076
./A +RTS -N2  2.14s user 0.02s system 149% cpu 1.449 total
Congratulations! You are now programming your multicore!

5 Where to go from here

There are many good Haskell tutorials and books. Here are some we recommend:
Tutorials
For a complete list of textbooks, references and tutorials:
Join the community!
Talk to others in the Haskell community.
Languages: de tur zh/cn zh/tw ja

How to install GHC on Ubuntu

#Install prerequisites
sudo aptitude install ghc darcs zlib1g-dev libncurses5-dev
#Get GHC 7.4.1 source and cabal-install HEAD
wget http://www.haskell.org/ghc/dist/7.4.1/ghc-7.4.1-src.tar.bz2
darcs get --lazy http://darcs.haskell.org/cabal/
 
tar xjf ghc-7.4.1-src.tar.bz2
cd ghc-7.4.1
./configure --prefix=$HOME/src/ghc
time make -j9 #Only took me about 19 minutes on recent quad core xeon
make install
 
echo "export PATH=`pwd`/bin:$HOME/.cabal/bin:$PATH" >> ~/.bashrc
#my bashfu might be off here, but you get the idea
. ~/.bashrc
 
mv ~/.cabal ~/.cabalold
cd ../cabal/cabal-install
export EXTRA_CONFIGURE_OPTS="-p"
./bootstrap
 
cabal update
#Edit ~/.cabal/config to set library profiling to True
 
#Test everything up until now
ghc --version
cabal --version
 
#to pull your own 'haskell platform' try the following
cabal install haskell-src html mtl parallel parsec \
regex-base regex-compat regex-posix stm syb text \
transformers vector --solver=modular
#I had to leave out GLUT, OpenGL, fgl, QuickCheck, and cgi
#to overcome some MonadCatchIO based dependency errors
 
 
 
In order to get Pandoc and some of the other stuff I need try this
cabal install vector-strategies vector-fftw fgl graphviz
# In the future instead of the below I think I'll be able to do...
# cd ~/src
# cabal unpack pandoc-types
# cd pandoc-types
# Here I had to edit Text/Pandoc/Builder.hs to hide ((<>)) from Data.Monoid
cabal install pandoc Graphalyze
Also I need to email the maintainers of Graphalyze and gnuplot about the changes I had to make to get it to compile with GHC 7.4.1.
 

Installing the Haskell Platform in Ubuntu

Installing the Haskell Platform in Ubuntu

First things first, lets get the dependencies out of the way.
1sudo apt-get install libedit2 libedit-dev freeglut3-dev libglu1-mesa-dev
Now let’s remove any haskell packages we may have installed, as these will cause conflicts.

1sudo apt-get autoremove ghc6
Note that if you installed cabal as per my previous post, you can remove the executable from your $PATH by executing the following:

1sudo rm /usr/local/sbin/cabal

This will not delete the executable, which is stored in $HOME/.cabal/bin/. This just removes the sym-link that was in your $PATH. Next, lets grab the source for GHC 6.10.3 (which is required to build the Haskell Platform)

and untar it.

next, lets configure it and build it:

1cd ghc-6.10.3/
2./configure
3sudo make install
After this, installing the Haskell Platform is just more of the same:

01#grab the tarball
03 
04# untar it
06 
07 
08# ... and install it.
09cd haskell-platform-2009.2.0.1/
10./configure
11make
12sudo make install
Along the way, the install scripts for the Haskell Platform will give you prompts for the next step to take.
Enjoy your Haskell Platform, and happy developing!

Haskell Platform for Linux

< Home
Community-supported versions of the Haskell Platform on Linux and Unix
These distributions offer the Haskell Platform in their package repositories. You can easily install the Haskell Platform through your distribution's native package manager.
Ubuntu Debian Fedora
Arch Linux Gentoo NixOS
OpenBSD FreeBSD Mint
Information for other systems
openSUSE Mandriva
See also: justhub, for REHL, CentOS, Scientific Linux, and Fedora spport

Build from source

Download the source tarball for Unix-like systems: here
Get and install GHC 7.4.2 prior to building the platform:
Finally, unpack the Haskell Platform source tarball, and run (possibly with 'sudo'):
    ./configure
    make
    make install
You may pass --prefix flag to ./configure to change the default install path.
There is also a README file in the tarball with more detailed information on building.

Prior releases

Haskell Platform for Linux

Haskell Platform
http://www.haskell.org/platform/
================================
 Download Link : http://www.haskell.org/platform/linux.html

This is the source tarball for Haskell Platform. It contains the source and
build files for all the packages that make up the platform, beyond those that
come with GHC.

If your OS has a pre-built distribution of Haskell Platform, that is likely to
be a much easier way than building the platform yourself. The Haskell Platform
web site (see above URL) contains builds for Mac OS X and Windows that include
everything you need, including GHC.

The web site also contains links for those Linux distributions for which there
is a pre-build version. In most cases, that version of the platform is availble
through the normal package manager for the distribution, rather than directly
from the Haskell Platform web site.



Preparation
-----------
You will need to have GHC 7.4.2 installed prior to building the platform.
Please see:
    http://www.haskell.org/ghc/download_ghc_7_4_2

Note that the warning at top will tell you to consider installing the
platform instead. If you've gotten this far, you can ignore that.

You can use a different version of GHC, but it may not work.


You will also need the developer packages for various standard system libraries,
such as zlib and GLUT. Exactly which packages you need will differ based on
the OS distribution you use. In general, if you get an error during the build
process that a library is missing, look it up in your package manager. You will
need both the library and the development package for the library.


Build and Install
-----------------
In this directory, run:

    ./configure

This is standard autoconf, so options like --prefix will work. Run with --help
to see all the options. In particular, if you are using a different versino of
GHC, you'll need to add --enable-unsupported-ghc-version.

Now build the packages:

    make

Finally, to install everything, use

    make install

Note that you will probably have to run this command 'sudo'.

Install Glasgow Haskell Compiler GHC on Ubuntu

Download GHC

http://www.haskell.org/ghc/download_ghc_7_4_2#x86linux 



This is the INSTALL instructions for a binary distribution of GHC. For
more details on what on earth this package is up to, please consult
the README and ANNOUNCE.

This distribution can be installed in a location of your choosing.

To set the ball rolling, run the configure script (as usual, run the
script with --help to see what options it supports).  eg. to set up
the package for installing in directory , use

    ./configure --prefix=

The default installation directory is /usr/local.

The configure script will figure out what platform you're running on,
and a couple of other interesting pieces of trivia, which it will then
fill in the Makefile.in template to give you a real Makefile.  If
you're of a paranoid persuasion, you might want to take a look at this
Makefile to see if the information is correct.

Now run:

    make install

(`make show-install-setup' prints the details of where the different
pieces of the bundle are heading when -- possibly helpful).

For more information, full GHC documentation is available from the
main GHC site:

  http://www.haskell.org/ghc

Bug reports/suggestions for improvement to the installation
procedure/setup (as well as other GHC related troubles you're
experiencing, of course), gratefully received.  Bug reporting
instructions are here:

  http://www.haskell.org/ghc/reportabug

Enjoy,
-- The GHC Team.