Lecture 10

### 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

## 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)

• /src

Folder for source-code
• Setup.hs

• app.cabal

Cabal build script

Informative text file
• LICENCE

Software licence statement

### Make a folder called `src`

Create a `Main.hs` file in /src

``````
-- Main.hs

module Main where

main = do
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
• 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
• LICENCE

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-0.1.0.0...
Building MyProj-0.1.0.0...
Preprocessing executable 'MyProj' for MyProj-0.1.0.0...
[1 of 1] Compiling Main
Installing executable(s) in /home/binroot/.cabal/bin
Installed MyProj-0.1.0.0

``````
Your executable is ready in `~/.cabal/bin`

## Documentation

Open up `Main.hs` and add some documentation

``````
module Main where

-- |This is the main function
main :: IO()
main = do
name <- getLine
putStrLn ("Hi " ++ name)
``````

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

Run `cabal haddock --executables`

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

• LICENCE

Don't forget to document your code

# Solution

### 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