Haskelldstefan/cse130-winter18/...1yr 5yr 10yr 15yr 1,000,000 1 100 10,000 ks s Haskell goto...

30
Haskell

Transcript of Haskelldstefan/cse130-winter18/...1yr 5yr 10yr 15yr 1,000,000 1 100 10,000 ks s Haskell goto...

Page 1: Haskelldstefan/cse130-winter18/...1yr 5yr 10yr 15yr 1,000,000 1 100 10,000 ks s Haskell goto intro.hs Title hs-nutshell Created Date 1/26/2018 1:13:30 AM ...

Haskell

Page 2: Haskelldstefan/cse130-winter18/...1yr 5yr 10yr 15yr 1,000,000 1 100 10,000 ks s Haskell goto intro.hs Title hs-nutshell Created Date 1/26/2018 1:13:30 AM ...

What is Haskell?

a typed, lazy, purely functional language

Page 3: Haskelldstefan/cse130-winter18/...1yr 5yr 10yr 15yr 1,000,000 1 100 10,000 ks s Haskell goto intro.hs Title hs-nutshell Created Date 1/26/2018 1:13:30 AM ...

Haskell is statically-typed

Page 4: Haskelldstefan/cse130-winter18/...1yr 5yr 10yr 15yr 1,000,000 1 100 10,000 ks s Haskell goto intro.hs Title hs-nutshell Created Date 1/26/2018 1:13:30 AM ...

Haskell is statically-typed

• Everything has a type

• Everything must make sense at compile time

➤ Unlike JavaScript where f(x) with f=undefined will not complain until you actually evaluate f(x)

• Is JavaScript typed?

➤ A: yes, B: no

Page 5: Haskelldstefan/cse130-winter18/...1yr 5yr 10yr 15yr 1,000,000 1 100 10,000 ks s Haskell goto intro.hs Title hs-nutshell Created Date 1/26/2018 1:13:30 AM ...

Haskell is statically-typed

• Everything has a type

• Everything must make sense at compile time

➤ Unlike JavaScript where f(x) with f=undefined will not complain until you actually evaluate f(x)

• Is JavaScript typed?

➤ A: yes, B: no

Page 6: Haskelldstefan/cse130-winter18/...1yr 5yr 10yr 15yr 1,000,000 1 100 10,000 ks s Haskell goto intro.hs Title hs-nutshell Created Date 1/26/2018 1:13:30 AM ...

Haskell is statically-typed

• Everything has a type

• Everything must make sense at compile time

➤ Unlike JavaScript where f(x) with f=undefined will not complain until you actually evaluate f(x)

• Is JavaScript typed?

➤ A: yes, B: no yes, but at runtime (dynamically)

Page 7: Haskelldstefan/cse130-winter18/...1yr 5yr 10yr 15yr 1,000,000 1 100 10,000 ks s Haskell goto intro.hs Title hs-nutshell Created Date 1/26/2018 1:13:30 AM ...

Why is this cool?

Page 8: Haskelldstefan/cse130-winter18/...1yr 5yr 10yr 15yr 1,000,000 1 100 10,000 ks s Haskell goto intro.hs Title hs-nutshell Created Date 1/26/2018 1:13:30 AM ...

Why is this cool?

• Removes whole classes of bugs

• Address bugs early vs. after they have been triggered

➤ Prevent weird errors from creeping up on you

➤ Important for safety, security, and compositionally

• Easier to optimize and write faster code

➤ You can remove your typeof checks; compiler can do fast things. V8 relies on types to makes things fast!

Page 9: Haskelldstefan/cse130-winter18/...1yr 5yr 10yr 15yr 1,000,000 1 100 10,000 ks s Haskell goto intro.hs Title hs-nutshell Created Date 1/26/2018 1:13:30 AM ...

Haskell is functional

• Does functional imply no“side-effects”?

➤ A: yes, B: no

Page 10: Haskelldstefan/cse130-winter18/...1yr 5yr 10yr 15yr 1,000,000 1 100 10,000 ks s Haskell goto intro.hs Title hs-nutshell Created Date 1/26/2018 1:13:30 AM ...

Haskell is functional

• Does functional imply no“side-effects”?

➤ A: yes, B: no

Page 11: Haskelldstefan/cse130-winter18/...1yr 5yr 10yr 15yr 1,000,000 1 100 10,000 ks s Haskell goto intro.hs Title hs-nutshell Created Date 1/26/2018 1:13:30 AM ...

Haskell is functional

Page 12: Haskelldstefan/cse130-winter18/...1yr 5yr 10yr 15yr 1,000,000 1 100 10,000 ks s Haskell goto intro.hs Title hs-nutshell Created Date 1/26/2018 1:13:30 AM ...

Haskell is functional

• Support for high-order, first-class functions

• Meaning of programs centered around:

➤ evaluating expressions

➤ not executing instructions

Page 13: Haskelldstefan/cse130-winter18/...1yr 5yr 10yr 15yr 1,000,000 1 100 10,000 ks s Haskell goto intro.hs Title hs-nutshell Created Date 1/26/2018 1:13:30 AM ...

Haskell is pure

• Expressions (e.g., functions) don’t have “side effects”

➤ Is JavaScript pure? A: yes, B: no

Page 14: Haskelldstefan/cse130-winter18/...1yr 5yr 10yr 15yr 1,000,000 1 100 10,000 ks s Haskell goto intro.hs Title hs-nutshell Created Date 1/26/2018 1:13:30 AM ...

Haskell is pure

• Expressions (e.g., functions) don’t have “side effects”

➤ Is JavaScript pure? A: yes, B: no

• Everything is immutable: mutation is a side-effect!

Page 15: Haskelldstefan/cse130-winter18/...1yr 5yr 10yr 15yr 1,000,000 1 100 10,000 ks s Haskell goto intro.hs Title hs-nutshell Created Date 1/26/2018 1:13:30 AM ...

Haskell is pure

• Expressions (e.g., functions) don’t have “side effects”

➤ Is JavaScript pure? A: yes, B: no

• Everything is immutable: mutation is a side-effect!

Page 16: Haskelldstefan/cse130-winter18/...1yr 5yr 10yr 15yr 1,000,000 1 100 10,000 ks s Haskell goto intro.hs Title hs-nutshell Created Date 1/26/2018 1:13:30 AM ...

Haskell is pure

• Expressions (e.g., functions) don’t have “side effects”

➤ Is JavaScript pure? A: yes, B: no

• Everything is immutable: mutation is a side-effect!

• What does it mean for an expression to not have side-effects?

➤ In a scope where x1, …, xn are defined, all occurrences of e (where FV(e) = {x1, …, xn})have the same value

Page 17: Haskelldstefan/cse130-winter18/...1yr 5yr 10yr 15yr 1,000,000 1 100 10,000 ks s Haskell goto intro.hs Title hs-nutshell Created Date 1/26/2018 1:13:30 AM ...

Why is this cool?

Don’t take it from me, take it from Backus

Page 18: Haskelldstefan/cse130-winter18/...1yr 5yr 10yr 15yr 1,000,000 1 100 10,000 ks s Haskell goto intro.hs Title hs-nutshell Created Date 1/26/2018 1:13:30 AM ...

Why is this cool?

• Algebraic laws: equational reasoning & optimizations

➤ Can always replace things that are equal, λ calculus!

• Easier to think about

➤ e.g., don’t need to worry if x changed after calling f

• Parallelism

➤ Can evaluate expressions in parallel!

Page 19: Haskelldstefan/cse130-winter18/...1yr 5yr 10yr 15yr 1,000,000 1 100 10,000 ks s Haskell goto intro.hs Title hs-nutshell Created Date 1/26/2018 1:13:30 AM ...

Haskell is lazy

Page 20: Haskelldstefan/cse130-winter18/...1yr 5yr 10yr 15yr 1,000,000 1 100 10,000 ks s Haskell goto intro.hs Title hs-nutshell Created Date 1/26/2018 1:13:30 AM ...

Haskell is lazy

• You don’t evaluate an expression until its result is absolutely necessary: in contrast to JavaScript

➤ Remember: call-by-name

• Haskell’s evaluation strategy is called call-by-need

➤ Because of the other properties: you actually only evaluate an expression once and cache the result

➤ Can you cache results in JavaScript? A: yes, B: no

Page 21: Haskelldstefan/cse130-winter18/...1yr 5yr 10yr 15yr 1,000,000 1 100 10,000 ks s Haskell goto intro.hs Title hs-nutshell Created Date 1/26/2018 1:13:30 AM ...

Haskell is lazy

• You don’t evaluate an expression until its result is absolutely necessary: in contrast to JavaScript

➤ Remember: call-by-name

• Haskell’s evaluation strategy is called call-by-need

➤ Because of the other properties: you actually only evaluate an expression once and cache the result

➤ Can you cache results in JavaScript? A: yes, B: no

Page 22: Haskelldstefan/cse130-winter18/...1yr 5yr 10yr 15yr 1,000,000 1 100 10,000 ks s Haskell goto intro.hs Title hs-nutshell Created Date 1/26/2018 1:13:30 AM ...

Why is this cool?

Page 23: Haskelldstefan/cse130-winter18/...1yr 5yr 10yr 15yr 1,000,000 1 100 10,000 ks s Haskell goto intro.hs Title hs-nutshell Created Date 1/26/2018 1:13:30 AM ...

Why is this cool?

• Can define your own control structures using functions

➤ E.g., defining if-then-else is much easier in Haskell and can be done naturally

➤ Less so in JavaScript, why?

• Can define infinite data structures

➤ E.g., infinite lists, trees, etc.

➤ Can solve general problem and then project solution

Page 24: Haskelldstefan/cse130-winter18/...1yr 5yr 10yr 15yr 1,000,000 1 100 10,000 ks s Haskell goto intro.hs Title hs-nutshell Created Date 1/26/2018 1:13:30 AM ...

Haskell is a committee language

Page 25: Haskelldstefan/cse130-winter18/...1yr 5yr 10yr 15yr 1,000,000 1 100 10,000 ks s Haskell goto intro.hs Title hs-nutshell Created Date 1/26/2018 1:13:30 AM ...

Why is this interesting? [SPJ]

1yr 5yr 10yr 15yr

1,000,000

1

100

10,000

Gee

ksPr

actit

ione

rs

C, C

++, J

avaS

cript

, Rub

y, ...

Page 26: Haskelldstefan/cse130-winter18/...1yr 5yr 10yr 15yr 1,000,000 1 100 10,000 ks s Haskell goto intro.hs Title hs-nutshell Created Date 1/26/2018 1:13:30 AM ...

Why is this interesting? [SPJ]

1yr 5yr 10yr 15yr

1,000,000

1

100

10,000

Gee

ksPr

actit

ione

rs

Research languages

Page 27: Haskelldstefan/cse130-winter18/...1yr 5yr 10yr 15yr 1,000,000 1 100 10,000 ks s Haskell goto intro.hs Title hs-nutshell Created Date 1/26/2018 1:13:30 AM ...

Why is this interesting? [SPJ]

1yr 5yr 10yr 15yr

1,000,000

1

100

10,000

Gee

ksPr

actit

ione

rs

Successful research languages

Page 28: Haskelldstefan/cse130-winter18/...1yr 5yr 10yr 15yr 1,000,000 1 100 10,000 ks s Haskell goto intro.hs Title hs-nutshell Created Date 1/26/2018 1:13:30 AM ...

Why is this interesting? [SPJ]

1yr 5yr 10yr 15yr

1,000,000

1

100

10,000

Gee

ksPr

actit

ione

rs

Committee languages

Page 29: Haskelldstefan/cse130-winter18/...1yr 5yr 10yr 15yr 1,000,000 1 100 10,000 ks s Haskell goto intro.hs Title hs-nutshell Created Date 1/26/2018 1:13:30 AM ...

Why is this interesting? [SPJ]

1yr 5yr 10yr 15yr

1,000,000

1

100

10,000

Gee

ksPr

actit

ione

rs

Haskell

Page 30: Haskelldstefan/cse130-winter18/...1yr 5yr 10yr 15yr 1,000,000 1 100 10,000 ks s Haskell goto intro.hs Title hs-nutshell Created Date 1/26/2018 1:13:30 AM ...

goto intro.hs