Haskell lightning talk

In [1]:
putStrLn $ "Hi There" ++ ": welcome to my talk"
Hi There: welcome to my talk

I'm going to tell you about the Haskell progamming language

Haskell is:

  • Functional
  • Strongly Typed
  • Lazy

Tour of Features

Functional

Focus on using functions to express ideas.

To that end, functions are first class citizens and there are higher order functions.

In [2]:
negate 5
-5
In [3]:
map negate [1, 2, 3, 4, 5]
[-1,-2,-3,-4,-5]
In [4]:
import Data.Char (toUpper)
toUpper 'a'
'A'
In [5]:
toUpperS = map toUpper
toUpperS "foobar"
"FOOBAR"
In [6]:
addBang str = str ++ "!"
In [7]:
shout = addBang . toUpperS
shout "haskell is fun"
"HASKELL IS FUN!"

Strongly typed

Types help you express ideas, read code, and…

The compiler can catch your errors!

In [8]:
:t 'a'
'a' :: Char
In [9]:
:t "Haskell"
"Haskell" :: [Char]
In [10]:
:t (.)
(.) :: forall b c a. (b -> c) -> (a -> b) -> a -> c
In [11]:
:t (+)
(+) :: forall a. Num a => a -> a -> a
In [12]:
:t map
map :: forall a b. (a -> b) -> [a] -> [b]

Lazy

Values aren't calculated until you need them

Can work on infinite lists

In [13]:
[1..4]
[1,2,3,4]
In [14]:
zip [1..] ['a'..'z']
[(1,'a'),(2,'b'),(3,'c'),(4,'d'),(5,'e'),(6,'f'),(7,'g'),(8,'h'),(9,'i'),(10,'j'),(11,'k'),(12,'l'),(13,'m'),(14,'n'),(15,'o'),(16,'p'),(17,'q'),(18,'r'),(19,'s'),(20,'t'),(21,'u'),(22,'v'),(23,'w'),(24,'x'),(25,'y'),(26,'z')]

Miscellaneous

Currying/Partial application

In [15]:
map (+2) [1..5]
[3,4,5,6,7]

Simple syntax

But sweetened with sugar!

e.g. list comprehensions

let's get all the sum of each multimple of 3 from 1 to 50 with each multiple of 7 from 1 to 50:

In [16]:
[ x + y | x <- [1..50], y <- [1..50], x `mod` 3 == 0, y `mod` 7 == 0 ]
[10,17,24,31,38,45,52,13,20,27,34,41,48,55,16,23,30,37,44,51,58,19,26,33,40,47,54,61,22,29,36,43,50,57,64,25,32,39,46,53,60,67,28,35,42,49,56,63,70,31,38,45,52,59,66,73,34,41,48,55,62,69,76,37,44,51,58,65,72,79,40,47,54,61,68,75,82,43,50,57,64,71,78,85,46,53,60,67,74,81,88,49,56,63,70,77,84,91,52,59,66,73,80,87,94,55,62,69,76,83,90,97]

Cool software

Diagrams

In [17]:
import Diagrams.Prelude
import Diagrams.Backend.Cairo
In [18]:
demo0 :: Diagram B
demo0 = circle 1 # fc yellow
demo0
In [19]:
demo1 :: Diagram B
demo1 = square 1 # fc blue # lw none `atop` circle 1 # fc yellow
demo1

Okay that's fun, but let's make a diagram based on some computation result

First, we'll get a list of random numbers. n.b. I'm seeding the random number generator myself for convenience.

In [20]:
import System.Random
rlist :: [Double]
rlist = randoms $ mkStdGen 17

And now create a diagram:

In [21]:
rcircles :: Diagram B
rcircles = hcat $ map circle $ take 7 rlist
rcircles

Learning resources

Links