40
Haskell Tjeerd Hans Terpstra Microsoft Consultant Atos

Haskell @ HAN Arnhem 2013-2014

Embed Size (px)

DESCRIPTION

These are cherry-picked slides from Simon Peyton Jones' excellent tutorial on Haskell at http://research.microsoft.com/en-us/um/people/simonpj/papers/haskell-tutorial/index.htm I prepared these for a guest lecture at the Hogeschool Arnhem-Nijmegen. If this piqued your interest, please have a look at SPJ's video's at the above link!

Citation preview

Page 1: Haskell @ HAN Arnhem 2013-2014

HaskellTjeerd Hans TerpstraMicrosoft Consultant

Atos

Page 2: Haskell @ HAN Arnhem 2013-2014

Agenda

• Introduction• History of Haskell• Real world Haskell• I/O• Step back• Project Euler

Page 3: Haskell @ HAN Arnhem 2013-2014

Introduction – who?

• Microsoft Consultant @ Atos• Microsoft Competence Leader for Atos East NL• Developer, Team lead, Scrum Master

@tjeerdhans

Page 4: Haskell @ HAN Arnhem 2013-2014

Introduction – who too?

• A Taste of Haskell - http://research.microsoft.com/en-us/um/people/simonpj/papers/haskell-tutorial/index.htm

Simon Peyton JonesResearcher at Microsoft Research (Cambridge, England)

http://research.microsoft.com/en-us/people/simonpj/

Page 5: Haskell @ HAN Arnhem 2013-2014

Introduction – what?

• Haskell is a programming language that is• purely functional• lazy• higher order• strongly typed• general purpose

Page 6: Haskell @ HAN Arnhem 2013-2014

Introduction – why?

• Functional programming will make you think differently about programming• Mainstream languages are all about state• Functional programming is all about values

• Whether or not you drink the Haskell whisky, you'll be a better programmer in whatever language you regularly use.

Page 7: Haskell @ HAN Arnhem 2013-2014

History of Haskell

Page 8: Haskell @ HAN Arnhem 2013-2014

History of Haskell

Page 9: Haskell @ HAN Arnhem 2013-2014

History of Haskell 3

Page 10: Haskell @ HAN Arnhem 2013-2014

History of Haskell 4

Page 11: Haskell @ HAN Arnhem 2013-2014

Real world Haskell© xkcd http://xkcd.com/1312/

Page 12: Haskell @ HAN Arnhem 2013-2014

Real world Haskell

• Text-based tools• Imperative input/output programming• Parsing, transforming data

• GUI programming• Event driven programming• Hook up events to callback functions in Haskell

• Database programming• Network programming

• Examples: http://book.realworldhaskell.org/read

Page 13: Haskell @ HAN Arnhem 2013-2014

Real world Haskell – xmonad

• xmonad is an X11 tiling window manager written entirely in Haskell

xmo..-wut?! DEMO!

Page 14: Haskell @ HAN Arnhem 2013-2014

Real world Haskell – xmonad

• xmonad is an X11 tiling window manager written entirely in Haskell

Page 15: Haskell @ HAN Arnhem 2013-2014

Why xmonad?

• Because it's • A real program• of manageable size• that illustrates many Haskell programming techniques• is open-source software• is being actively developed by an active community

Page 16: Haskell @ HAN Arnhem 2013-2014

Manageable size

Code Comments Language

metacity >50000 C

ion3 20000 700 C

larswm 6000 130 C

wmii 6000 100 C

dwm 4.2 1500 200 C

xmonad 0.2 500 700 Haskell

Page 17: Haskell @ HAN Arnhem 2013-2014

Inside xmonad

Page 18: Haskell @ HAN Arnhem 2013-2014
Page 19: Haskell @ HAN Arnhem 2013-2014
Page 20: Haskell @ HAN Arnhem 2013-2014
Page 21: Haskell @ HAN Arnhem 2013-2014
Page 22: Haskell @ HAN Arnhem 2013-2014
Page 23: Haskell @ HAN Arnhem 2013-2014
Page 24: Haskell @ HAN Arnhem 2013-2014
Page 25: Haskell @ HAN Arnhem 2013-2014
Page 26: Haskell @ HAN Arnhem 2013-2014

Things to notice

• Purity makes the interface explicit

• Takes a stack, and returns a stack; that is all

• Takes a stack; may modify it; may modify other persistent state; may do I/O

Page 27: Haskell @ HAN Arnhem 2013-2014

I/O

Page 28: Haskell @ HAN Arnhem 2013-2014

Where’s the I/O in xmonad?• All this pure stuff is nice, but sooner or later we

have to:• Talk to X11, whose interface is not at all pure• Do input/output (other programs)

Page 29: Haskell @ HAN Arnhem 2013-2014

Doing I/O

• Idea:

• But: nowmight do arbitrary stateful things • And what does this do?

• What order are the things printed?• Are they printed at all?

Order of evaluation!

Laziness!

Page 30: Haskell @ HAN Arnhem 2013-2014

The main idea

A value of type (IO t) is an “action” that, when performed, may do some input/output

before delivering a result of type t

Page 31: Haskell @ HAN Arnhem 2013-2014

A helpful picture

A value of type (IO t) is an “action” that, when performed, may do some input/output

before delivering a result of type t

Page 32: Haskell @ HAN Arnhem 2013-2014

Simple I/O

getLine :: IO StringputStr :: String -> IO ()

main :: IO ()Main = putStr “Hello World”

Main program is an action of type IO ()

Page 33: Haskell @ HAN Arnhem 2013-2014

Connecting actions up

Goal:read a line and then write it back out

Page 34: Haskell @ HAN Arnhem 2013-2014

Connecting actions up

We have connected two actions tomake a new, bigger action.

Page 35: Haskell @ HAN Arnhem 2013-2014

Getting two lines

We want to just return (s1,s2)

Page 36: Haskell @ HAN Arnhem 2013-2014

The return combinator

Page 37: Haskell @ HAN Arnhem 2013-2014

Taking a step back

The central challenge

Page 38: Haskell @ HAN Arnhem 2013-2014

The challenge of effects

Page 39: Haskell @ HAN Arnhem 2013-2014

Project Euler

http://projecteuler.net

Page 40: Haskell @ HAN Arnhem 2013-2014

Thanks