Transcript
Page 1: Haskell Tour (Part 1)

HaskellA Whirlwind Tour

William Taysom2011

Page 2: Haskell Tour (Part 1)

Haskell

Page 3: Haskell Tour (Part 1)

Haskell

Who?

What?

When?

Where?

Why?

How?

Page 4: Haskell Tour (Part 1)

Haskell

Who?

What?

When?

Where?

Why?

How?

Page 5: Haskell Tour (Part 1)

Why?

Aesthetics

Page 6: Haskell Tour (Part 1)

Why? Aesthetics

“We provided DARPA with a copy of our prototype implemented in Haskell without explaining that it was a program, and based on preconceptions from their past experience, they had studied the program under the assumption that it was a mixture of requirements specification and top level design. They were convinced it was incomplete because it did not address issues such as data structure design and execution order.”

— Paul Hudak

Page 7: Haskell Tour (Part 1)

Why? Aesthetics

“Take Lisp: you know it's the most beautiful language in the world. At least up until Haskell came along.”

— Larry Wall

Page 8: Haskell Tour (Part 1)

Why? Aesthetics

“Reading Haskell is like reading poetry.Writing Haskell is like writing poetry.”

— Oliver Steele

Page 9: Haskell Tour (Part 1)

Why? Aesthetics

“I also have interest in Haskell, but my brain just explodes every time I read a Haskell program bigger than ten lines.”

— Matz

Page 10: Haskell Tour (Part 1)

Why? Aesthetics

“The biggest advantage of Haskell to me is that it helps me write better programs in other languages.”

— Tenerife Skunkworks

Page 11: Haskell Tour (Part 1)

Why? Aesthetics

.NET LINQ

Python List Comprehensions

Java Generics

JavaScript jQuery

Page 12: Haskell Tour (Part 1)

Why?

Pragmatics

Page 13: Haskell Tour (Part 1)

Why? Pragmatics

Platform Compiler Debugger Profiler Testing

Libraries Network Graphics Hackage

Community Books Documentation Hoogle

Page 14: Haskell Tour (Part 1)

Why? Pragmatics

Platform Compiler Debugger Profiler Testing

Libraries Network Graphics Hackage

Community Books Documentation Hoogle

Page 15: Haskell Tour (Part 1)

Why? PragmaticsHoogle

Page 16: Haskell Tour (Part 1)

Why? PragmaticsHoogle

Page 17: Haskell Tour (Part 1)

Why? Pragmatics

Platform Compiler Debugger Profiler Testing

Libraries Network Graphics Hackage

Community Books Documentation Hoogle

Page 18: Haskell Tour (Part 1)

Why? Pragmatics

Platform Compiler Debugger Profiler Testing

Libraries Network Graphics Hackage

Community Books Documentation Hoogle

Page 19: Haskell Tour (Part 1)

How?

GHCDocumentation

LibrariesCabal

Hackage

Page 20: Haskell Tour (Part 1)

Why? Pragmatics

Platform Compiler Debugger Profiler Testing

Libraries Network Graphics Hackage

Community Books Documentation Hoogle

Page 21: Haskell Tour (Part 1)

Why? Pragmatics

Page 22: Haskell Tour (Part 1)

Why? Pragmatics

Page 23: Haskell Tour (Part 1)

Why? Pragmatics

“A programming language must be considered in the context of its community, and Haskell has an exemplary one. I have come to believe, however, that this polite exterior conceals a deep and consuming madness.”

— Avdi Grimm

Page 24: Haskell Tour (Part 1)

Why?

Performance

Page 25: Haskell Tour (Part 1)

Why? PerformanceThe Computer Language Benchmarks Game

Page 26: Haskell Tour (Part 1)

Web ServerPong benchmark, extra large instance, requests/second

Why? Performance

Page 27: Haskell Tour (Part 1)

Web ServerPong benchmark, extra large instance, requests/second

Why? Performance

Page 28: Haskell Tour (Part 1)

Why?

AestheticsPragmatics

Performance

Page 29: Haskell Tour (Part 1)

Haskell

Who?

What?

When?

Where?

Why? Aesthetics Pragmatics Performance

How?

Page 30: Haskell Tour (Part 1)

Haskell

Who?

What?

When?

Where?

Why? Aesthetics Pragmatics Performance

How?

Page 31: Haskell Tour (Part 1)

Who? When? Where?

September 1987

Page 32: Haskell Tour (Part 1)

Who? When? Where?

Portland, Oregon

Page 33: Haskell Tour (Part 1)

Who? When? Where?

Functional Programming Languages and Computer Architecture Conference

Page 34: Haskell Tour (Part 1)

Who? When? Where?

A Dozen PurelyFunctional Languages

Page 35: Haskell Tour (Part 1)

Who? When? Where?

All Similar

Page 36: Haskell Tour (Part 1)

Who? When? Where? Committee Formed

Page 37: Haskell Tour (Part 1)

Who? When? Where? Committee Formed

Page 38: Haskell Tour (Part 1)
Page 39: Haskell Tour (Part 1)

Provide faster communication of new ideas.

Stable foundation for real application development.

Vehicle through which others would be encouraged to use functional languages.

Page 40: Haskell Tour (Part 1)

Who? When? Where?

Haskell ReportApril 1st 1990

“You know, Haskell actually never liked the name Haskell.”

— Mary Curry

Page 41: Haskell Tour (Part 1)

Who? When? Where?

2002

Revised Haskell 98 Report

Page 42: Haskell Tour (Part 1)

Who? When? Where?

2010

Haskell'

Page 43: Haskell Tour (Part 1)

Haskell

Who? Research Wadler Hudak Peyton-Jones

What?

When? 1987 1990 2002 Now

Where? Portland Glasgow Microsoft

Why? Aesthetics Pragmatics Performance

How?

Page 44: Haskell Tour (Part 1)

Haskell

Who? Research Wadler Hudak Peyton-Jones

What?

When? 1987 1990 2002 Now

Where? Portland Glasgow Microsoft

Why? Aesthetics Pragmatics Performance

How?

Page 45: Haskell Tour (Part 1)

What?

A non-strict, purely functional programming language with strong,static type inference.

Page 46: Haskell Tour (Part 1)

What?

A non-strict, purely functional programming language with strong,static type inference.

Page 47: Haskell Tour (Part 1)

What? Programming

Source Code

Formal, Textural Syntax

Static & Runtime Semantics

Data, Variables, Lexical Scope

Interpreter, Compiler

Page 48: Haskell Tour (Part 1)

What?

A non-strict, purely functional programming language with strong,static type inference.

Page 49: Haskell Tour (Part 1)

What?

A non-strict, purely functional programming language with strong,static type inference.

Page 50: Haskell Tour (Part 1)

What? Functional

(Not Imperative)

Page 51: Haskell Tour (Part 1)

What? Functional

Imperative Statements Executed Step-by-step

Page 52: Haskell Tour (Part 1)

What? Functional

Imperative Statements Executed Step-by-step

Modifying State

Page 53: Haskell Tour (Part 1)

What? Functional

Imperative Statements Executed Step-by-step

Modifying State

Turing Machine

Page 54: Haskell Tour (Part 1)

What? Functional

Imperative Statements Executed Step-by-step

Modifying State

Von Neumann Architecture

Page 55: Haskell Tour (Part 1)

What? Functional

Imperative Statements Executed Step-by-step

Modifying State

Von Neumann Architecture

Functional Expressions Recursively Simplified

Page 56: Haskell Tour (Part 1)

What? Functional

Imperative Statements Executed Step-by-step

Modifying State

Von Neumann Architecture

Functional Expressions Recursively Simplified

Reduced Value

Page 57: Haskell Tour (Part 1)

What? Functional

Imperative Statements Executed Step-by-step

Modifying State

Von Neumann Architecture

Functional Expressions Recursively Simplified

Reduced Value

Lambda Calculus

Page 58: Haskell Tour (Part 1)

Imperative Statements Executed Step-by-step

Modifying State

Von Neumann Architecture

Functional Expressions Recursively Simplified

Reduced Value

Lambda Calculus

What? Functional

Page 59: Haskell Tour (Part 1)

Imperative Statements Executed Step-by-step

Modifying State

Von Neumann Architecture

Functional Expressions Recursively Simplified

Reduced Value

Lambda Calculus

What? Functional

Page 60: Haskell Tour (Part 1)

What?

A non-strict, purely functional programming language with strong,static type inference.

Page 61: Haskell Tour (Part 1)

What?

A non-strict, purely functional programming language with strong,static type inference.

Page 62: Haskell Tour (Part 1)

What? Pure

(No Side Effects)

Page 63: Haskell Tour (Part 1)

What? Pure

Immutable Only

Page 64: Haskell Tour (Part 1)

What? Pure

Referential Transparency

Page 65: Haskell Tour (Part 1)

What? Pure

Functions alwaysreturn the same value.

If v = f x, then you canalways replace f x with v.

Page 66: Haskell Tour (Part 1)

What? Pure

Functions alwaysreturn the same value.

If v equals f x, then you canalways replace f x with v.

Page 67: Haskell Tour (Part 1)

What?

A non-strict, purely functional programming language with strong,static type inference.

Page 68: Haskell Tour (Part 1)

What?

A non-strict, purely functional programming language with strong,static type inference.

Page 69: Haskell Tour (Part 1)

What? Non-strict

Be lazy.

Page 70: Haskell Tour (Part 1)

What? Non-strict

Be lazy.

Ignore evaluation order.

Page 71: Haskell Tour (Part 1)

Wait...

Page 72: Haskell Tour (Part 1)

if everything is immutable and there

are no side effects and evaluation is lazy,

Page 73: Haskell Tour (Part 1)

then how the hell do you do anything?

Page 74: Haskell Tour (Part 1)

M O N A D SBring your own Semicolon

Page 75: Haskell Tour (Part 1)

What? Monads

Page 76: Haskell Tour (Part 1)

What? Monads

Page 77: Haskell Tour (Part 1)

What? Monads

Page 78: Haskell Tour (Part 1)

What? Monads

Page 79: Haskell Tour (Part 1)

What? Monads

Page 80: Haskell Tour (Part 1)

What? Monads

“Haskell is the world's finestimperative programming language.”

— Simon Peyton-Jones

Page 81: Haskell Tour (Part 1)

What? Monads

“Haskell is the only language I know withfirst-class support for imperative programming.”

— SamB

Page 82: Haskell Tour (Part 1)

What? Monads

“Haskell has no preferred imperative semantics, and the monad just lets you swap out the semantics according to your needs.”

— Jared Updike

Page 83: Haskell Tour (Part 1)

What?

A non-strict, purely functional programming language with strong,static type inference.

Page 84: Haskell Tour (Part 1)

What?

A non-strict, purely functional programming language with strong,static type inference.

Page 85: Haskell Tour (Part 1)

What? Types

StrongStatic

Inference

Page 86: Haskell Tour (Part 1)

What? Types

StrongStatic

Inference

Page 87: Haskell Tour (Part 1)

What? Strong Types

Runtime valueshave types.

Page 88: Haskell Tour (Part 1)

What? Strong Types

Runtime valueshave types.

like Java and Rubyunlike C and Assembly

(Not Weak)

Page 89: Haskell Tour (Part 1)

What? Types

StrongStatic

Inference

Page 90: Haskell Tour (Part 1)

What? Types

StrongStatic

Inference

Page 91: Haskell Tour (Part 1)

What? Static Types

Source code expressionshave types.

Page 92: Haskell Tour (Part 1)

What? Static Types

Source code expressionshave types.

like C and Javaunlike Ruby and JavaScript

(Not Dynamic)

Page 93: Haskell Tour (Part 1)

What? Types

StrongStatic

Inference

Page 94: Haskell Tour (Part 1)

What? Types

StrongStatic

Inference

Page 95: Haskell Tour (Part 1)

What? Type Inference

Automatically determines types of variables.

Page 96: Haskell Tour (Part 1)

What? Type Inference

Automatically determines types of variables.

like C# and Gounlike C and Java

(Not Manifest)

Page 97: Haskell Tour (Part 1)

You don’t need totype the type!

Page 98: Haskell Tour (Part 1)

S Y N E R G YPurity means types tell you a lot.

Page 99: Haskell Tour (Part 1)

What? Type Purity

No side effectsmean,

Page 100: Haskell Tour (Part 1)

What? Type Purity

No side effectsmean,

argument and returntypes limit what

a function can do.

Page 101: Haskell Tour (Part 1)

What? Type Purity

“Haskell is so strict about type safety that randomly generated snippets of code that successfully type check are likely to do something useful, even if you've no idea what that useful thing is.”

— sigfpe

Page 102: Haskell Tour (Part 1)

What? Type Purity

“Since when does "it compiles" equate to "it will run (correctly)"? We're talking about C, after all, not Haskell.”

— Sean Russell

Page 103: Haskell Tour (Part 1)

What? Types

StrongStatic

Inference

Page 104: Haskell Tour (Part 1)

What?

A non-strict, purely functional programming language with strong,static type inference.

Page 105: Haskell Tour (Part 1)

What?

A non-strict, purely functional programming language with strong,static type inference.

Page 106: Haskell Tour (Part 1)

Haskell

Who? Research Wadler Hudak Peyton-Jones

What? Non-strict Purely Functional Static Types

When? 1987 1990 2002 Now

Where? Portland Glasgow Microsoft

Why? Aesthetics Pragmatics Performance

How?

Page 107: Haskell Tour (Part 1)

Haskell

Who? Research Wadler Hudak Peyton-Jones

What? Non-strict Purely Functional Static Types

When? 1987 1990 2002 Now

Where? Portland Glasgow Microsoft

Why? Aesthetics Pragmatics Performance

How?

Page 108: Haskell Tour (Part 1)

How?

Just Downloadand Install

the Platform

Page 109: Haskell Tour (Part 1)

How?

Just Downloadand Install

the Platform

Page 110: Haskell Tour (Part 1)

Glorious Glasgow Haskell Compilation

System

Page 111: Haskell Tour (Part 1)

GHC

Compiler ghc

Interactive ghci

Scripts runghc

Page 112: Haskell Tour (Part 1)

GHC

Compiler ghc

Interactive ghci

Scripts runghc

Page 113: Haskell Tour (Part 1)

Haskell

Who? Research Wadler Hudak Peyton-Jones

What? Non-strict Purely Functional Static Types

When? 1987 1990 2002 Now

Where? Portland Glasgow Microsoft

Why? Aesthetics Pragmatics Performance

How? Platform Hoogle Hackage

Page 114: Haskell Tour (Part 1)

Haskell

Who? Research Wadler Hudak Peyton-Jones

What? Non-strict Purely Functional Static Types

When? 1987 1990 2002 Now

Where? Portland Glasgow Microsoft

Why? Aesthetics Pragmatics Performance

How? Platform Hoogle Hackage

Page 115: Haskell Tour (Part 1)

Example

Page 116: Haskell Tour (Part 1)

Example

┼ $

Page 117: Haskell Tour (Part 1)

$ ghci

Page 118: Haskell Tour (Part 1)

$ ghciGHCi, version 7.0.2: http://www.haskell.org/ghc/ :? for helpLoading package ghc-prim ... linking ... done.Loading package integer-gmp ... linking ... done.Loading package base ... linking ... done.Loading package ffi-1.0 ... linking ... done.ghci>

Page 119: Haskell Tour (Part 1)

$ ghciGHCi, version 7.0.2: http://www.haskell.org/ghc/ :? for helpLoading package ghc-prim ... linking ... done.Loading package integer-gmp ... linking ... done.Loading package base ... linking ... done.Loading package ffi-1.0 ... linking ... done.ghci> "hello, world"

Page 120: Haskell Tour (Part 1)

$ ghciGHCi, version 7.0.2: http://www.haskell.org/ghc/ :? for helpLoading package ghc-prim ... linking ... done.Loading package integer-gmp ... linking ... done.Loading package base ... linking ... done.Loading package ffi-1.0 ... linking ... done.ghci> "hello, world""hello, world"ghci>

Page 121: Haskell Tour (Part 1)

"hello, world"ghci>

Page 122: Haskell Tour (Part 1)

"hello, world"ghci> 6 * 9

Page 123: Haskell Tour (Part 1)

"hello, world"ghci> 6 * 942ghci>

Page 124: Haskell Tour (Part 1)

"hello, world"ghci> 6 * 942ghci> [1, 2, 3]

Page 125: Haskell Tour (Part 1)

"hello, world"ghci> 6 * 942ghci> [1, 2, 3][1,2,3]ghci>

Page 126: Haskell Tour (Part 1)

"hello, world"ghci> 6 * 942ghci> [1, 2, 3][1,2,3]ghci> it

Page 127: Haskell Tour (Part 1)

"hello, world"ghci> 6 * 942ghci> [1, 2, 3][1,2,3]ghci> it[1,2,3]ghci>

Page 128: Haskell Tour (Part 1)

“Function application is so important in Haskell that we denote it using the quietest possible syntax: nothing at all.”

— Simon Peyton-Jones

"hello, world"ghci> 6 * 942ghci> [1, 2, 3][1,2,3]ghci> it[1,2,3]ghci> reverse it

Page 129: Haskell Tour (Part 1)

“Function application is so important in Haskell that we denote it using the quietest possible syntax: nothing at all.”

— Simon Peyton-Jones

Page 130: Haskell Tour (Part 1)

“Function application is so important in Haskell that we denote it using the quietest possible syntax: nothing at all.”

— Simon Peyton-Jones

"hello, world"ghci> 6 * 942ghci> [1, 2, 3][1,2,3]ghci> it[1,2,3]ghci> reverse it

Page 131: Haskell Tour (Part 1)

"hello, world"ghci> 6 * 942ghci> [1, 2, 3][1,2,3]ghci> it[1,2,3]ghci> reverse it[3,2,1]ghci>

Page 132: Haskell Tour (Part 1)

[3,2,1]ghci>

Page 133: Haskell Tour (Part 1)

[3,2,1]ghci> let xs = [1..100]

Page 134: Haskell Tour (Part 1)

[3,2,1]ghci> let xs = [1..100]ghci>

Page 135: Haskell Tour (Part 1)

[3,2,1]ghci> let xs = [1..100]ghci> xs

Page 136: Haskell Tour (Part 1)

[3,2,1]ghci> let xs = [1..100]ghci> xs[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100]ghci>

Page 137: Haskell Tour (Part 1)

[3,2,1]ghci> let xs = [1..100]ghci> xs[1,2,⋯,99,100]ghci>

Page 138: Haskell Tour (Part 1)

[3,2,1]ghci> let xs = [1..100]ghci> xs[1,2,⋯,99,100]ghci> [x | x <- xs, x > 21]

Page 139: Haskell Tour (Part 1)

[3,2,1]ghci> let xs = [1..100]ghci> xs[1,2,⋯,99,100]ghci> [x | x <- xs, x > 21][22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100]ghci>

Page 140: Haskell Tour (Part 1)

[3,2,1]ghci> let xs = [1..100]ghci> xs[1,2,⋯,99,100]ghci> [x | x <- xs, x > 21][22,23,⋯,99,100]ghci>

Page 141: Haskell Tour (Part 1)

[3,2,1]ghci> let xs = [1..100]ghci> xs[1,2,⋯,99,100]ghci> [x | x <- xs, x > 21][22,23,⋯,99,100]ghci> filter (> 21) xs

Page 142: Haskell Tour (Part 1)

[3,2,1]ghci> let xs = [1..100]ghci> xs[1,2,⋯,99,100]ghci> [x | x <- xs, x > 21][22,23,⋯,99,100]ghci> filter (> 21) xs[22,23,⋯,99,100]ghci>

Page 143: Haskell Tour (Part 1)

[3,2,1]ghci> let xs = [1..100]ghci> xs[1,2,⋯,99,100]ghci> [x | x <- xs, x > 21][22,23,⋯,99,100]ghci> filter (> 21) xs[22,23,⋯,99,100]ghci> filter (\x -> x > 21) xs

Page 144: Haskell Tour (Part 1)

TMTOWTDI

[3,2,1]ghci> let xs = [1..100]ghci> xs[1,2,⋯,99,100]ghci> [x | x <- xs, x > 21][22,23,⋯,99,100]ghci> filter (> 21) xs[22,23,⋯,99,100]ghci> filter (\x -> x > 21) xs[22,23,⋯,99,100]ghci>

Page 145: Haskell Tour (Part 1)

TMTOWTDI

Page 146: Haskell Tour (Part 1)

1. Freshman2. Sophomore3. Junior (Peano)4. Junior (Ban n+k)5. Senior (Leans Right)6. Senior (Leans Left)7. Senior (Leans Around)8. Memoizing9. Points-free10. Iterative11. Iterative one-liner12. Accumulating

13. Continuation-passing14. Boy Scout15. Combinatory16. List-encoding17. Interpretive18. Static19. Beginning Graduate20. Origamist21. Cartesianally-inclined22. Ph.D.23. Post-doc24. Tenured Professor

The Evolution of a Haskell Programmer

Page 147: Haskell Tour (Part 1)

TMTOWTDI

Page 148: Haskell Tour (Part 1)

TMTOWTDI

[3,2,1]ghci> let xs = [1..100]ghci> xs[1,2,⋯,99,100]ghci> [x | x <- xs, x > 21][22,23,⋯,99,100]ghci> filter (> 21) xs[22,23,⋯,99,100]ghci> filter (\x -> x > 21) xs[22,23,⋯,99,100]ghci>

Page 149: Haskell Tour (Part 1)

[22,23,⋯,99,100]ghci>

Page 150: Haskell Tour (Part 1)

[22,23,⋯,99,100]ghci> let divides = \x y -> rem y x == 0

Page 151: Haskell Tour (Part 1)

[22,23,⋯,99,100]ghci> let divides = \x y -> rem y x == 0ghci>

Page 152: Haskell Tour (Part 1)

[22,23,⋯,99,100]ghci> let divides = \x y -> rem y x == 0ghci> let divides x y = rem y x == 0

Page 153: Haskell Tour (Part 1)

[22,23,⋯,99,100]ghci> let divides = \x y -> rem y x == 0ghci> let divides x y = rem y x == 0ghci>

Page 154: Haskell Tour (Part 1)

[22,23,⋯,99,100]ghci> let divides = \x y -> rem y x == 0ghci> let divides x y = rem y x == 0ghci> divides 3 12

Page 155: Haskell Tour (Part 1)

[22,23,⋯,99,100]ghci> let divides = \x y -> rem y x == 0ghci> let divides x y = rem y x == 0ghci> divides 3 12Trueghci>

Page 156: Haskell Tour (Part 1)

[22,23,⋯,99,100]ghci> let divides = \x y -> rem y x == 0ghci> let divides x y = rem y x == 0ghci> divides 3 12Trueghci> 3 `divides` 12

Page 157: Haskell Tour (Part 1)

[22,23,⋯,99,100]ghci> let divides = \x y -> rem y x == 0ghci> let divides x y = rem y x == 0ghci> divides 3 12Trueghci> 3 `divides` 12Trueghci>

Page 158: Haskell Tour (Part 1)

[22,23,⋯,99,100]ghci> let divides = \x y -> rem y x == 0ghci> let divides x y = rem y x == 0ghci> divides 3 12Trueghci> 3 `divides` 12Trueghci> let divisors x = [d | d <- [1..x], d `divides` x]

Page 159: Haskell Tour (Part 1)

[22,23,⋯,99,100]ghci> let divides = \x y -> rem y x == 0ghci> let divides x y = rem y x == 0ghci> divides 3 12Trueghci> 3 `divides` 12Trueghci> let divisors x = [d | d <- [1..x], d `divides` x]ghci>

Page 160: Haskell Tour (Part 1)

[22,23,⋯,99,100]ghci> let divides = \x y -> rem y x == 0ghci> let divides x y = rem y x == 0ghci> divides 3 12Trueghci> 3 `divides` 12Trueghci> let divisors x = [d | d <- [1..x], d `divides` x]ghci> divisors 100

Page 161: Haskell Tour (Part 1)

Fundamentals

[22,23,⋯,99,100]ghci> let divides = \x y -> rem y x == 0ghci> let divides x y = rem y x == 0ghci> divides 3 12Trueghci> 3 `divides` 12Trueghci> let divisors x = [d | d <- [1..x], d `divides` x]ghci> divisors 100[1,2,4,5,10,20,25,50,100]ghci>

Page 162: Haskell Tour (Part 1)

Fundamentals

Page 163: Haskell Tour (Part 1)

Data Declaration

data Color = Red | Green | Blue

Page 164: Haskell Tour (Part 1)

Variables

red = Red-- Lower case for variable and upper case for constructor.

nan = 0 / 0-- Variables stand for values. They do not label locations.-- Don't need let because we aren't at GHCi.-- Declarations go at top-level, not expressions. (Like Java, unlike Ruby.)

Page 165: Haskell Tour (Part 1)

Functions

hue Red = 0hue Green = 120hue Blue = 240

Page 166: Haskell Tour (Part 1)

Lambda & Case

hue = \c -> case c of Red -> 0 Green -> 120 Blue -> 240

Page 167: Haskell Tour (Part 1)

Pattern Wildcard

isRed Red = TrueisRed _ = False

Page 168: Haskell Tour (Part 1)

Boolean Data

data Bool = True | False

Page 169: Haskell Tour (Part 1)

Boolean Functions

otherwise = True

not True = Falsenot False = True

Page 170: Haskell Tour (Part 1)

Boolean Operators

True && x = xFalse && _ = False

(||) True _ = True(||) False x = x-- Parenthesis let us use operators prefix.

Page 171: Haskell Tour (Part 1)

Operator Precedence

infixr 3 &&infixr 2 ||

-- Ten precedence levels: 0 binds least tightly, 9 (default) binds most tightly.

-- Three associativities: infixl (default), infixr, infix (non-associative).

Page 172: Haskell Tour (Part 1)

Types

“Types in Haskell express high-level design in the same way that UML diagrams do in Object Oriented languages.”

— Simon Peyton-Jones

Page 173: Haskell Tour (Part 1)

Types

“Types in Haskell express high-level design in the same way that UML diagrams do in Object Oriented languages.”

— Simon Peyton-Jones

ghci> :l example

Page 174: Haskell Tour (Part 1)

ghci> :l example[1 of 1] Compiling Main ( example.hs, interpreted )Ok, modules loaded: Main.ghci>

Page 175: Haskell Tour (Part 1)

ghci> :l example[1 of 1] Compiling Main ( example.hs, interpreted )Ok, modules loaded: Main.ghci> :r

Page 176: Haskell Tour (Part 1)

ghci> :l example[1 of 1] Compiling Main ( example.hs, interpreted )Ok, modules loaded: Main.ghci> :rOk, modules loaded: Main.ghci>

Page 177: Haskell Tour (Part 1)

ghci> :l example[1 of 1] Compiling Main ( example.hs, interpreted )Ok, modules loaded: Main.ghci> :rOk, modules loaded: Main.ghci> :type red

Page 178: Haskell Tour (Part 1)

ghci> :l example[1 of 1] Compiling Main ( example.hs, interpreted )Ok, modules loaded: Main.ghci> :rOk, modules loaded: Main.ghci> :type redRed :: Colorghci>

Page 179: Haskell Tour (Part 1)

ghci> :l example[1 of 1] Compiling Main ( example.hs, interpreted )Ok, modules loaded: Main.ghci> :rOk, modules loaded: Main.ghci> :type redRed :: Colorghci> :t isRed

Page 180: Haskell Tour (Part 1)

red :: Colornan :: Double

hue :: Color -> Double

otherwise :: Boolnot :: Bool -> Bool(&&), (||) :: Bool -> Bool -> Bool

Typesghci> :l example[1 of 1] Compiling Main ( example.hs, interpreted )Ok, modules loaded: Main.ghci> :rOk, modules loaded: Main.ghci> :type redRed :: Colorghci> :t isRedisRed :: Color -> Boolghci>

Page 181: Haskell Tour (Part 1)

red :: Colornan :: Double

hue :: Color -> Double

otherwise :: Boolnot :: Bool -> Bool(&&), (||) :: Bool -> Bool -> Bool

Types

Page 182: Haskell Tour (Part 1)

Recursive Data

data Color = Red | Green | Blue | Mix Color Color

Page 183: Haskell Tour (Part 1)

Recursive Data

data Color = Red | Green | Blue | Mix Color Color

mix :: Color -> Color -> Colormix = Mix-- Mix is a type constructor function.

Page 184: Haskell Tour (Part 1)

Recursive Data

yellow, cyan, magenta :: Coloryellow = Mix Red Green

Mix cyan magenta = Mix (Mix Green Blue) (Mix Red Blue)-- Constructor functions can be used for pattern matching, but variables bind.

Page 185: Haskell Tour (Part 1)

Recursive Functions

isRed :: Color -> BoolisRed Red = TrueisRed (Mix c c') = isRed c && isRed c'isRed _ = False

Page 186: Haskell Tour (Part 1)

Recursive Functions

hue :: Color -> Doublehue Red = 0hue Green = 120hue Blue = 240

Page 187: Haskell Tour (Part 1)

Recursive Functions

hue :: Color -> Doublehue Red = 0hue Green = 120hue Blue = 240

hue (Mix c c') = ???

Page 188: Haskell Tour (Part 1)

hue (Mix c c') = ???

Page 189: Haskell Tour (Part 1)

h

hue (Mix c c') = ???

Page 190: Haskell Tour (Part 1)

h

hue (Mix c c') = let h = hue c ???

Page 191: Haskell Tour (Part 1)

h

hue (Mix c c') = let h = hue c ???

h'

Page 192: Haskell Tour (Part 1)

h

hue (Mix c c') = let h = hue c h' = hue c' ???

h'

Page 193: Haskell Tour (Part 1)

h

hue (Mix c c') = let h = hue c h' = hue c' ???

h' m

Page 194: Haskell Tour (Part 1)

hue (Mix c c') = let h = hue c h' = hue c' m = average h h' ???

h

h' m

Page 195: Haskell Tour (Part 1)

h

h' mhue (Mix c c') = let h = hue c h' = hue c' m = average h h' average x y = abs (x + y) / 2 ???

Page 196: Haskell Tour (Part 1)

h

h' mhue (Mix c c') = let h = hue c h' = hue c' m = average h h' average x y = abs (x + y) / 2 in m

Page 197: Haskell Tour (Part 1)

h

h' mhue (Mix c c') = let h = hue c h' = hue c' m = average h h' average x y = abs (x + y) / 2 in ??!

Page 198: Haskell Tour (Part 1)

h

m

h'

hue (Mix c c') = let h = hue c h' = hue c' m = average h h' average x y = abs (x + y) / 2 in ??!

Page 199: Haskell Tour (Part 1)

h

m

h'

hue (Mix c c') = let h = hue c h' = hue c' m = average h h' average x y = abs (x + y) / 2 in ??!

m'

Page 200: Haskell Tour (Part 1)

h

m

h' m'

hue (Mix c c') = let h = hue c h' = hue c' m = average h h' m' = m + 180 average x y = abs (x + y) / 2 in ??!

Page 201: Haskell Tour (Part 1)

h

m

h' m'

hue (Mix c c') = let h = hue c h' = hue c' m = average h h' m' = m + 180 average x y = abs (x + y) / 2 in ??!

d

Page 202: Haskell Tour (Part 1)

h

m

h' m'

dhue (Mix c c') = let h = hue c h' = hue c' m = average h h' m' = m + 180 d = distance h m average x y = abs (x + y) / 2 in ??!

Page 203: Haskell Tour (Part 1)

h

m

h' m'

dhue (Mix c c') = let h = hue c h' = hue c' m = average h h' m' = m + 180 d = distance h m average x y = abs (x + y) / 2 distance x y = abs (x - y) in ??!

Page 204: Haskell Tour (Part 1)

h

m

h' m'

dhue (Mix c c') = let h = hue c h' = hue c' m = average h h' m' = m + 180 d = distance h m average x y = abs (x + y) / 2 distance x y = abs (x - y) in case compare d 90 of LT -> m EQ -> ??! GT -> m'

Page 205: Haskell Tour (Part 1)

h

m

h' m'

dhue (Mix c c') = let h = hue c h' = hue c' m = average h h' m' = m + 180 d = distance h m average x y = abs (x + y) / 2 distance x y = abs (x - y) in case compare d 90 of LT -> m EQ -> nan GT -> m'

Page 206: Haskell Tour (Part 1)

h

m

h' m'

dhue (Mix c c') = r where r = case compare d 90 of LT -> m EQ -> nan GT -> m' h = hue c h' = hue c' m = average h h' m' = m + 180 d = distance h m average x y = abs (x + y) / 2distance x y = abs (x - y)

Page 207: Haskell Tour (Part 1)

test-framework organize tests

HUnit what you’re used to

QuickCheck test properties with

automatically generated data

Testing

Page 208: Haskell Tour (Part 1)

QuickCheck

prop_hue_bounds c = let h = hue c in isNaN h || 0 <= h && h < 360

Page 209: Haskell Tour (Part 1)

QuickCheck

prop_hue_bounds c = let h = hue c in isNaN h || 0 <= h && h < 360

prop_hue_mix_reflexivity c = let h = hue c in isNaN h || hue (Mix c c) == h

Page 210: Haskell Tour (Part 1)

QuickCheck

prop_hue_bounds c = let h = hue c in isNaN h || 0 <= h && h < 360

prop_hue_mix_reflexivity c = let h = hue c in isNaN h || hue (Mix c c) == h

prop_hue_mix_commutativity c c' = let h = hue (Mix c c') in isNaN h || hue (Mix c' c) == h

Page 211: Haskell Tour (Part 1)

QuickCheck

prop_hue_bounds c = let h = hue c in isNaN h || 0 <= h && h < 360

prop_hue_mix_reflexivity c = let h = hue c in isNaN h || hue (Mix c c) == h

prop_hue_mix_commutativity c c' = let h = hue (Mix c c') in isNaN h || hue (Mix c' c) == h

ghci> quickCheck prop_hue_mix_commutativity

Page 212: Haskell Tour (Part 1)

ghci> quickCheck prop_hue_mix_commutativity+++ OK, passed 100 tests.ghci>

Page 213: Haskell Tour (Part 1)

ghci> quickCheck prop_hue_mix_commutativity+++ OK, passed 100 tests.ghci> quickCheck prop_hue_mix_reflexivity

Page 214: Haskell Tour (Part 1)

ghci> quickCheck prop_hue_mix_commutativity+++ OK, passed 100 tests.ghci> quickCheck prop_hue_mix_reflexivity+++ OK, passed 100 tests.ghci>

Page 215: Haskell Tour (Part 1)

ghci> quickCheck prop_hue_mix_commutativity+++ OK, passed 100 tests.ghci> quickCheck prop_hue_mix_reflexivity+++ OK, passed 100 tests.ghci> quickCheck prop_hue_bounds

Page 216: Haskell Tour (Part 1)

ghci> quickCheck prop_hue_mix_commutativity+++ OK, passed 100 tests.ghci> quickCheck prop_hue_mix_reflexivity+++ OK, passed 100 tests.ghci> quickCheck prop_hue_bounds*** Failed! Falsifiable (after 3 tests): Mix (Mix Red Blue) (Mix Green Red)ghci>

Page 217: Haskell Tour (Part 1)

ghci> quickCheck prop_hue_mix_commutativity+++ OK, passed 100 tests.ghci> quickCheck prop_hue_mix_reflexivity+++ OK, passed 100 tests.ghci> quickCheck prop_hue_bounds*** Failed! Falsifiable (after 3 tests): Mix (Mix Red Blue) (Mix Green Red)ghci> hue (Mix magenta yellow)

Page 218: Haskell Tour (Part 1)

ghci> quickCheck prop_hue_mix_commutativity+++ OK, passed 100 tests.ghci> quickCheck prop_hue_mix_reflexivity+++ OK, passed 100 tests.ghci> quickCheck prop_hue_bounds*** Failed! Falsifiable (after 3 tests): Mix (Mix Red Blue) (Mix Green Red)ghci> hue (Mix magenta yellow)360.0ghci>

Page 219: Haskell Tour (Part 1)

hue (Mix c c') = r where r = case compare d 90 of LT -> m EQ -> nan GT -> m' h = hue c h' = hue c' m = average h h' m' = m + 180 d = distance h m

m'm

hd

h'

Page 220: Haskell Tour (Part 1)

hue (Mix c c') = r where r = case compare d 90 of LT -> m EQ -> nan GT -> m' h = hue c h' = hue c' m = average h h' m' = normalize (m + 180) d = distance h m

m'm

hd

h'

Page 221: Haskell Tour (Part 1)

hue (Mix c c') = r where r = case compare d 90 of LT -> m EQ -> nan GT -> m' h = hue c h' = hue c' m = average h h' m' = normalize (m + 180) d = distance h m

normalize h | h < 360 = h | otherwise = h - 360

m'm

hd

h'

Page 222: Haskell Tour (Part 1)

hue (Mix c c') = r where r = case compare d 90 of LT -> m EQ -> nan GT -> m' h = hue c h' = hue c' m = average h h' m' = normalize (m + 180) d = distance h m

normalize h | h < 360 = h | otherwise = normalize (h - 360)

m'm

hd

h'

Page 223: Haskell Tour (Part 1)

hue (Mix c c') = r where r = case compare d 90 of LT -> m EQ -> nan GT -> m' h = hue c h' = hue c' m = average h h' m' = normalize (m + 180) d = distance h m

normalize h | h < 360 = h | otherwise = normalize (h - 360)

m'm

hd

h'ghci>

Page 224: Haskell Tour (Part 1)

m'

ghci> quickCheck prop_hue_bounds

Page 225: Haskell Tour (Part 1)

m'

ghci> quickCheck prop_hue_bounds+++ OK, passed 100 tests.ghci>

Page 226: Haskell Tour (Part 1)

prop_hue_mix_nothing c c' = distance (hue c) (hue c') == 180 ==> isNaN (hue (Mix c c'))

QuickCheck

Page 227: Haskell Tour (Part 1)

QuickCheck

prop_hue_mix_nothing c c' = distance (hue c) (hue c') == 180 ==> isNaN (hue (Mix c c'))

ghci> quickCheck prop_hue_mix_nothing

Page 228: Haskell Tour (Part 1)

QuickCheck

prop_hue_mix_nothing c c' = distance (hue c) (hue c') == 180 ==> isNaN (hue (Mix c c'))

ghci> quickCheck prop_hue_mix_nothing*** Gave up! Passed only 23 tests.ghci>

Page 229: Haskell Tour (Part 1)

QuickCheck

prop_hue_mix_nothing c c' = distance (hue c) (hue c') == 180 ==> isNaN (hue (Mix c c'))

Page 230: Haskell Tour (Part 1)

QuickCheck

prop_hue_mix_nothing c c' = distance (hue c) (hue c') == 180 ==> isNaN (hue (Mix c c'))

-- Can we easily find the complement of a color?

Page 231: Haskell Tour (Part 1)

complement Red = ???complement Green = ???complement Blue = ???complement (Mix c c') = ???

Page 232: Haskell Tour (Part 1)

complement Red = cyancomplement Green = ???complement Blue = ???complement (Mix c c') = ???

Page 233: Haskell Tour (Part 1)

complement Red = cyancomplement Green = magentacomplement Blue = ???complement (Mix c c') = ???

Page 234: Haskell Tour (Part 1)

complement Red = cyancomplement Green = magentacomplement Blue = yellowcomplement (Mix c c') = ???

Page 235: Haskell Tour (Part 1)

complement Red = cyancomplement Green = magentacomplement Blue = yellowcomplement (Mix c c') = Mix ??? ???

Page 236: Haskell Tour (Part 1)

complement Red = cyancomplement Green = magentacomplement Blue = yellowcomplement (Mix c c') = Mix (complement c ) (complement c' )

Page 237: Haskell Tour (Part 1)

QuickCheck

prop_complement c = let h = hue c in not (isNaN h) ==> distance h (hue (complement c)) == 180

Page 238: Haskell Tour (Part 1)

QuickCheck

prop_complement c = let h = hue c in not (isNaN h) ==> distance h (hue (complement c)) == 180

prop_hue_mix_complement c = isNaN (hue (Mix c (complement c)))

Page 239: Haskell Tour (Part 1)

QuickCheck

prop_complement c = let h = hue c in not (isNaN h) ==> distance h (hue (complement c)) == 180

prop_hue_mix_complement c = isNaN (hue (Mix c (complement c)))

ghci> quickCheck prop_complement

Page 240: Haskell Tour (Part 1)

ghci> quickCheck prop_complement+++ OK, passed 100 tests.ghci>

Page 241: Haskell Tour (Part 1)

ghci> quickCheck prop_complement+++ OK, passed 100 tests.ghci> quickCheck prop_hue_mix_complement

Page 242: Haskell Tour (Part 1)

ghci> quickCheck prop_complement+++ OK, passed 100 tests.ghci> quickCheck prop_hue_mix_complement+++ OK, passed 100 tests.ghci>

Page 243: Haskell Tour (Part 1)

To be continued...

Page 244: Haskell Tour (Part 1)

Summary

Haskell is functional.

Haskell has types.

QuickCheck is cool.

Page 245: Haskell Tour (Part 1)

Preview: Infinite Lists

primes = sieve [2..] where sieve (p:xs) = p : sieve [x | x <- xs, rem x p /= 0]

Page 246: Haskell Tour (Part 1)

Preview: IO (echo.hs)

import System.Environment (getArgs)import Data.List (intercalate)

main = do args <- getArgs putStrLn (intercalate " " args)

Page 247: Haskell Tour (Part 1)

Preview: Parsers

Page 248: Haskell Tour (Part 1)

Preview: Parse JSON

data Value = String String | Number Double | Object [(String, Value)] | Array [Value] | Bool Bool | Null

Page 249: Haskell Tour (Part 1)

Preview: Parse JSON

value = String <$> jsstring <|> Number <$> number <|> Object <$> commaGroup '{' pair '}' <|> Array <$> commaGroup '[' value ']' <|> Bool True <$ string "true" <|> Bool False <$ string "false" <|> Null <$ string "null"

Page 250: Haskell Tour (Part 1)

Preview: Parse JSON

pair :: Parser (String, Value)pair = do s <- jsstring sp_char_sp ':' v <- value spaces return (s, v)

Page 251: Haskell Tour (Part 1)

To be continued...


Top Related