Haskell
programming language
Haskell is…
Memory managed (allocation, collection)
“Typeful” (static, strong)
– Types are checked at compile-time
– Types cannot be coerced (in general)
Pure functional programming
– Emphasis on functions
– Referential transparency
– All variables are constant
Haskell - pros & cons
Concurrency
– The #1 on Language Shootout for threading
– All non-I/O code is concurrent by default
– (mutations are handled as I/O code)
Readability
– No parentheses or commas
– Higher-order functions reduce lines-of-code
– (no syntax as a reminder of context)
Haskell - namespaces
Main -- modules (packages)
main -- value variables (functions)
True -- value constructors
a -- type variables (generics)
Bool -- type constructors
Eq -- type classes (interfaces)
Haskell - syntax
Derived syntax Core syntax
f x = expr f = \x -> expr -- lambda
(x *) \y -> x * y -- sections
(* y) \x -> x * y -- sections
(*) \x y -> x * y -- sections
x `times` y times x y -- infix func
f $ g $ h x f (g (h x)) -- parens
(f . g . h) x f (g (h x)) -- compose
do a; b; c a >>= b >>= c -- I/O bind
Haskell - type syntax
life = 42 :: Int
life :: Int
life = 42
Haskell - types
“Hello” :: String
length :: [a] -> Int
floor :: Float -> Int
map :: (a -> b) -> [a] -> [b]
42 :: Int
(+) :: Int -> Int -> Int
42 :: Num a => a
(+) :: Num a => a -> a -> a
Haskell - type system
Int, Word, Float, Double, Char -- built-in types
type String = [Char] -- type synonyms
data Maybe a = Nothing | Just a -- data types
class Eq a where -- type classes
(==), (/=) :: a -> a -> Bool
instance Eq Bool where -- type class instances
True == True = True
False == False = True
_ == _ = False
Haskell - datatypes
data Bool = False | True -- enumerations
data Tree a = Leaf a -- generics
| Branch (Tree a) (Tree a)
data Rect = Rect Int Int Int Int -- unlabeled record
data Rect = Rect { -- labeled record
x, y :: Int,
width :: Int,
height :: Int}
Haskell - example programs
main = return () -- null program
main = putStrLn “Hello World” -- hello world
main = interact id -- UNIX cat
main = interact
-- GNU tac
(unlines . reverse . lines)
-- UNIX echo
main = do
args <- getArgs
case args of
"-n":a -> putStr (unwords a)
a -> putStrLn (unwords a)
version control system
Darcs - overview
The Theory of Patches
A “branch” is a set of patches
“Spontaneous branches”
Every checkout is a branch
Every checkout is a repository
Darcs - interactive
Interactive “pull”
– You chose what patches to download
Interactive “push”
– You chose what patches to commit externally
Interactive “record”
– You chose what files to commit locally
Parsec
parser library
Parsec - combinators
many :: Parser a -> Parser [a] -- like regex*
many1 :: Parser a -> Parser [a] -- like regex+
optional :: Parser a -> Parser (Maybe a) -- like regex?
sepBy :: Parser a -> Parser s -> Parser [a] -- ... , ... , ...
sepBy1 :: Parser a -> Parser s -> Parser [a]
endBy :: Parser a -> Parser s -> Parser [a] -- ... ; ... ; ... ;
endBy1 :: Parser a -> Parser s -> Parser [a]
char :: Char -> Parser Char -- c
between :: Parser open -- < ... >
-> Parser close
-> Parser a -> Parser a
Parsec - example parsers
number = many digit
string = between
(char ‘"’)
(char ‘"’)
(many anyChar)
lisp = number
<|> string
<|> identifier
<|> parens $ many $ lexeme lisp
xmonad
X11 window manager
XMonad - overview
Tilling window manager (like Ratpoison)
Libraries of extensions and status bars
Customizable (config file is the app)
Full keyboard accessibility
XMonad - example config
module Main where
import XMonad
import System.Exit
import qualified XMonad.StackSet as W
import qualified Data.Map as M
myKeys conf@(XConfig {XMonad.modMask = modMask}) = M.fromList $ [
((modMask .|. shiftMask, xK_Return), spawn $ XMonad.terminal conf),
((modMask .|. shiftMask, xK_q ), io (exitWith ExitSuccess))]
myMouseBindings (XConfig {XMonad.modMask = modMask}) = M.fromList $ [
((modMask, button1), (\w -> focus w >> mouseMoveWindow w)),
((modMask, button2), (\w -> focus w >> windows W.swapMaster)),
((modMask, button3), (\w -> focus w >> mouseResizeWindow w))]
defaults = defaultConfig {
keys = myKeys,
mouseBindings = myMouseBindings,
terminal = "xterm"}
main = xmonad defaults
Yi
extensible text editor
Yi - overview
Extensible
– Fully dynamic application (hs-plugins)
– All state is serialized and reloaded
Customizable
– yi --as=vim (for vim key bindings)
– yi --as=emacs (for emacs key bindings)
Yi - structure
Taken from <http://www.cse.unsw.edu.au/~dons/papers/SC05.html>
Haskell
Thank You