Introduction To Haskell Lecture 10

Haskell Development Methods

Using These Slides Every slide has a secret note. On Chrome : press F12 , then click Console

: press , then click On IE : press F12 , then click Console

: press , then click On Firefox: Ctrl+Shift+k



Shortcut Keys: ↓ , PgDn , n , j next slide ↑ , PgUp , p , k prev slide Esc enables ctrl+f globally Hi there! This is a secret lecture note. Every slide has a little blurb of text like this!

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 also called Point-less, but it is certainly not pointless. This notation emphasizes 'the what' rather than 'the how'.

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) As you can see, point-free changes the way your think. The implementation of the function is now just a composition of other functions.

Haskell File Structure /src Folder for source-code

Setup.hs Haskell build script

app.cabal Cabal build script

README Informative text file

LICENCE Software licence statement These are just convention.

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 Although we could simply run this code through ghc, running more complicated code should be done another way (demonstrated in the following slides).

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 cabal is the main tool we will use. `cabal` comes with the Haskell-platform. If you're on a Debian based machine, just do 'sudo apt-get install cabal-install'.

Almost done Your file structure should now look like: /src

Setup.hs

projectName .cabal

.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 You'll need to define the Main.hs file containing the main function.

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 Linking dist/build/MyProj/MyProj ... Installing executable(s) in /home/binroot/.cabal/bin Installed MyProj-0.1.0.0 Your executable is ready in ~/.cabal/bin We now have an executable file ready!

Documentation 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 -- | is the syntax for documenting function type signature, type declaration, or class declaration Sometimes the code speaks for itself, otherwise you should document the code.

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 It's similar to javadoc.

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

README

LICENCE



Don't forget to document your code Don't feel overwhelmed by the amount of files in your folder. The only files that really matter for you are in the /src folder.

Solution Here's an example.

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. Notice how you can understand most of the source code. If fact you can strip out functions from the source code and use them in your own projects. Haskell's type system is your best friend.

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. Version controlling is not just useful for yourself to keep track of changes, but also irreplaceable for working with a team of programmers.