Introduction To Haskell

Lecture 10

Haskell Development Methods

Using These Slides

Every slide has a secret note.

  • On Chrome: press F12, then click Console
  • On IE: press F12, then click Console
  • On Firefox: Ctrl+Shift+k

Shortcut Keys:

, PgDn, n, j next slide
, PgUp, p, k prev slide
Esc enables ctrl+f globally

Good Coding Style

Point-Free Notation

Some functions can be written without variables

add a b = a + b

add = (+)

f s = length (head (words s))

f = length.head.words

Compose functions (high level) rather than shuffle data (low level) *

Point-Free is Exciting!

Convert this piece of code to point-free notation

f x = 5 + 8/x

f x = (+) 5 ( (/) 8 x)		      

f x = ((+) 5) ( ((/) 8) x)

f x = ((+) 5) . ( ((/) 8) ) x

f = ((+) 5) . ( (/) 8 )

f = (5+) . (8/)

(solution by Prateek)

Haskell File Structure

  • /src

    Folder for source-code
  • Setup.hs

    Haskell build script
  • app.cabal

    Cabal build script

    Informative text file

    Software licence statement

Make a folder called src

Create a Main.hs file in /src

-- Main.hs

module Main where

main = do
  putStrLn "What's your name?"
  name <- getLine
  putStrLn ("Hi " ++ name)

Set Up the Rest

Return to the top directory, and run cabal init.

It helps you fill out the following information:

  • Package name
  • Package version
  • License
  • Author name
  • Maintainer email
  • Project homepage URL
  • Project synopsis
  • Build type

Here's an example of a good cabal file

Almost done

Your file structure should now look like:

  • /src
  • Setup.hs
  • projectName.cabal

Edit the last part of projectName.cabal:

executable MyProj
  main-is:             Main.hs
  -- other-modules:       
  build-depends:       base ==4.6.*
  hs-source-dirs:      src

That's it!

Run cabal install at the project directory

$ cabal install

Resolving dependencies...
Configuring MyProj-
Building MyProj-
Preprocessing executable 'MyProj' for MyProj-
[1 of 1] Compiling Main
Linking dist/build/MyProj/MyProj ...
Installing executable(s) in /home/binroot/.cabal/bin
Installed MyProj-

Your executable is ready in ~/.cabal/bin


Open up Main.hs and add some documentation

module Main where

-- |This is the main function
main :: IO()
main = do
  putStrLn "What's your name?"
  name <- getLine
  putStrLn ("Hi " ++ name)

-- | is the syntax for documenting function type signature, type declaration, or class declaration

Run Haddock

Haddock is a documentation engine.

Run cabal haddock --executables

Your documentation will be ready in dist/doc/html/MyProj/MyProj/index.html

Congratulations! Soon your documentation will look like this

Try it yourself

Rewrite the password-strength checker using the proper code structure

Make sure you have the following:

  • /src/Main.hs

  • Setup.hs

  • app.cabal



Don't forget to document your code


Learn from Others

You now have the skills to soak in other people's Haskell code

Check out this simple Snake game

Download it. Run cabal install and play around with the code.

Use Version Control!

Git is Awesome. Use Git.

If you're an uber-haxx0r, use Darcs

(It's written in Haskell)

Regardless, start using version control. Become a developer, not just a programmer.


Learn from Others

  1. Fill out this week's form!
  2. Find an interesting piece of Haskell code online and run it.

    Hint: Browse GitHub