Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Haskell - A brief introduction

Haskell - A brief introduction

Talk given by Fred van den Driessche on 13-06-2012 at MetaBroadcast.

MetaBroadcast

June 13, 2012
Tweet

More Decks by MetaBroadcast

Other Decks in Programming

Transcript

  1. Built-in Types Numbers: 1, 1.234 Booleans: True, False Characters/Strings: 'a',

    "asdf" Lists: [1,2,3] Tuples:("jim", 25) • • • • •
  2. Control max x y = if x > y then

    x else y max x y | x > y = x | otherwise y
  3. Lists [] 1 : [2, 3] = [1, 2, 3]

    [1, 2] ++ [3, 4] = [1, 2, 3, 4] head [1, 2, 3] = 1 tail [1, 2, 3] = [2, 3]g • • • • •
  4. Higher-order functions > map double [1, 2, 3] [2, 4,

    6] map f [] = [] map f (x:xs) = f x : map f xs
  5. Function Types > :t double double :: Num a =>

    a -> a > :t (*) (*) :: Num a => a -> a -> a > :t map map :: (a -> b) -> [a] -> [b]
  6. Currying > :t (*) (*) :: Num a => a

    -> a -> a > :t (2*) (2*) :: Num a => a -> a double = 2* dblAll = map (2*)
  7. Data Types data Bool = True | False data Maybe

    a = Just a | Nothing data Either a b = Left a | Right b data Tree a = Empty | Node a (Tree a) (Tree b ) • • • •
  8. yesOrNo True = "yes" yesOrNo False = "no" traverse Empty

    = [] traverse (Node v l r) = (traverse l) ++ v : traverse r
  9. Laziness fib c n = c : fib n (c

    + n) > fib 0 1 [0, 1, 1, 2, 3, 5, 8, 13, 21 ... > take 5 (fib 0 1) [0, 1, 1, 2, 3]