18
COS 326 Func,onal Programming: An elegant weapon for the modern age David Walker Princeton University © 2007 xkcd slides copyright 2013-2015 David Walker and Andrew W. Appel permission granted to reuse these slides for non-commercial educaFonal purposes

COS 326 Func,onal Programming: An elegant weapon for the ... · In 1936, Alonzo Church invented the lambda calculus. He called it a logic, but it was a language of pure funcFons --

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: COS 326 Func,onal Programming: An elegant weapon for the ... · In 1936, Alonzo Church invented the lambda calculus. He called it a logic, but it was a language of pure funcFons --

COS326Func,onalProgramming:Anelegantweaponforthemodernage

DavidWalkerPrincetonUniversity

©2007xkcd

slidescopyright2013-2015DavidWalkerandAndrewW.Appelpermissiongrantedtoreusetheseslidesfornon-commercialeducaFonalpurposes

Page 2: COS 326 Func,onal Programming: An elegant weapon for the ... · In 1936, Alonzo Church invented the lambda calculus. He called it a logic, but it was a language of pure funcFons --

In1936,AlonzoChurchinventedthelambdacalculus.Hecalleditalogic,butitwasalanguageofpurefuncFons--theworld'sfirstprogramminglanguage.Hesaid:"Theremay,indeed,beotherapplica3onsofthesystemthanitsuseasalogic."AlonzoChurch,1903-1995

PrincetonProfessor,1929-1967

2

Page 3: COS 326 Func,onal Programming: An elegant weapon for the ... · In 1936, Alonzo Church invented the lambda calculus. He called it a logic, but it was a language of pure funcFons --

In1936,AlonzoChurchinventedthelambdacalculus.Hecalleditalogic,butitwasalanguageofpurefuncFons--theworld'sfirstprogramminglanguage.Hesaid:"Theremay,indeed,beotherapplica3onsofthesystemthanitsuseasalogic."

Greatesttechnologicalunderstatementofthe20th

century?

AlonzoChurch,1903-1995PrincetonProfessor,1929-1967

3

Page 4: COS 326 Func,onal Programming: An elegant weapon for the ... · In 1936, Alonzo Church invented the lambda calculus. He called it a logic, but it was a language of pure funcFons --

AlonzoChurch1934--developedlambdacalculus

AlanTuring(PhDPrinceton1938)1936--developedTuringmachines

ProgrammingLanguages Computers

hZp://press.princeton.edu/chapters/s9780.pdfOp3onalreading:TheBirthofComputerScienceatPrincetoninthe1930sbyAndrewW.Appel,2012.

4

Page 5: COS 326 Func,onal Programming: An elegant weapon for the ... · In 1936, Alonzo Church invented the lambda calculus. He called it a logic, but it was a language of pure funcFons --

AfewdesignersoffuncFonalprogramminglanguages5

AlonzoChurch:λ-calculus,1934

JohnMcCarthy(PhDPrinceton1951)

LISP,1958

GuySteele&GerrySussman:Scheme,1975

RobinMilnerML,1978

XavierLeroy:Ocaml,1990’s

LucaCardelliEdinburghML,1981

StandardML1986

CaML1980s

Appel&MacQueen:SML/NJ,1988

Implementa,ons:

Page 6: COS 326 Func,onal Programming: An elegant weapon for the ... · In 1936, Alonzo Church invented the lambda calculus. He called it a logic, but it was a language of pure funcFons --

Theywereyoungerthantheyappear…6

AlonzoChurch:λ-calculus,1934

JohnMcCarthy(PhDPrinceton1951)

LISP,1958

GuySteele&GerrySussman:Scheme,1975

RobinMilnerML,1978

XavierLeroy:Ocaml,1990’s

LucaCardelliEdinburghML,1981

Appel&MacQueen:SML/NJ,1988

Implementa,ons:Photo~1960

Photo~1975

Photo~2005Photo~1995

Photo2005 Photo~2000

Photo~2005

Photo~1995 Photo~1995

Page 7: COS 326 Func,onal Programming: An elegant weapon for the ... · In 1936, Alonzo Church invented the lambda calculus. He called it a logic, but it was a language of pure funcFons --

VastlyAbbreviatedFPGeneologyLCFTheoremProver(70s)

EdinburghML

Miranda(80s)

Haskell(90s-now)

StandardML(90s-now) OCaml

(90s-now)

Caml(80s-now)

F#(now)

LISP(50s-now)

Scheme(70s-now)

lazy

typed,polymorphic

untyped

Coq(80s-now)

dependentlytyped

call-by-value

Racket(00s-now)

Scala(00s-now)

7

Page 8: COS 326 Func,onal Programming: An elegant weapon for the ... · In 1936, Alonzo Church invented the lambda calculus. He called it a logic, but it was a language of pure funcFons --

ButWhyFuncFonalProgrammingNow?

•  FuncFonalprogrammingwillintroduceyoutonewwaystothinkaboutandstructureyourprograms:–  newreasoningprinciples–  newabstracFons–  newdesignpaZerns–  newalgorithms–  elegantcode

•  Technologytrendspointtoincreasingparallelism:–  mulFcore,gpu,datacenter–  funcFonalprogrammingtechniquessuchasmap-reduceprovideaplausiblewayforwardformanyapplicaFons

8

Page 9: COS 326 Func,onal Programming: An elegant weapon for the ... · In 1936, Alonzo Church invented the lambda calculus. He called it a logic, but it was a language of pure funcFons --

FuncFonalLanguages:Who’susingthem?

F#inVisualStudio

map-reduceintheirdatacenters

Erlangforconcurrency,HaskellformanagingPHP

Haskelltosynthesizehardware

Scalaforcorrectness,maintainability,flexibility

www.arFma.com/scalazine/arFcles/twiZer_on_scala.htmlgregosuri.com/how-facebook-uses-erlang-for-real-Fme-chatwww.janestcapital.com/technology/ocaml.phpmsdn.microsos.com/en-us/fsharp/cc742182labs.google.com/papers/mapreduce.htmlwww.haskell.org/haskellwiki/Haskell_in_industry

HaskellforspecifyingequityderivaFves

mathemaFcians

Coq(re)proofof4-colortheorem

9

Page 10: COS 326 Func,onal Programming: An elegant weapon for the ... · In 1936, Alonzo Church invented the lambda calculus. He called it a logic, but it was a language of pure funcFons --

FuncFonalLanguages:Jointhecrowd•  ElementsoffuncFonalprogrammingareshowingupallover

–  F#inMicrososVisualStudio–  ScalacombinesML(afuncFonallanguage)withObjects

•  runsontheJVM–  C#includes“delegates”

•  delegates==funcFons–  Pythonincludes“lambdas”

•  lambdas==morefuncFons–  Javascript

•  findtutorialsonlineaboutusingfuncFonalprogrammingtechniquestowritemoreelegantcode

–  C++librariesformap-reduce•  enabledfuncFonalparallelismatGoogle

–  JavahasgenericsandGC–  ...

10

Page 11: COS 326 Func,onal Programming: An elegant weapon for the ... · In 1936, Alonzo Church invented the lambda calculus. He called it a logic, but it was a language of pure funcFons --

COURSELOGISTICS

11

Page 12: COS 326 Func,onal Programming: An elegant weapon for the ... · In 1936, Alonzo Church invented the lambda calculus. He called it a logic, but it was a language of pure funcFons --

CourseStaff

ChristopherMoreyTeachingFacultyHeadPreceptoroffice:COS208email:cmorey@

RobinQiuGradStudentoffice:Fishbowlemail:yqiu@cs

AndersMiltnerGradStudentoffice:Fishbowl

email:amiltner@cs

DavidWalkerProfessor

office:COS211email:dpw@

12

Page 13: COS 326 Func,onal Programming: An elegant weapon for the ... · In 1936, Alonzo Church invented the lambda calculus. He called it a logic, but it was a language of pure funcFons --

Resources•  Web:

–  hZp://www.cs.princeton.edu/~cos326

•  Lecturescheduleandreadings:–  $(coursehome)/lectures.php

•  Assignments:–  $(coursehome)/assignments.php

•  Precepts–  usefulifyouwanttodowellonexamsandhomeworks

•  InstallOCaml:$(coursehome)/resources.php

13

Page 14: COS 326 Func,onal Programming: An elegant weapon for the ... · In 1936, Alonzo Church invented the lambda calculus. He called it a logic, but it was a language of pure funcFons --

CollaboraFonPolicyTheCOS326collaboraFonpolicycanbefoundhere:

Readitinfullpriortobeginningthefirstassignment.PleaseaskquesFonswheneveranythingisunclear,atanyFmeduringthecourse.

hZp://www.cs.princeton.edu/courses/archive/fall13/cos326/info.php#collab

14

Page 15: COS 326 Func,onal Programming: An elegant weapon for the ... · In 1936, Alonzo Church invented the lambda calculus. He called it a logic, but it was a language of pure funcFons --

CourseTextbook

hZp://realworldocaml.org/

15

Page 16: COS 326 Func,onal Programming: An elegant weapon for the ... · In 1936, Alonzo Church invented the lambda calculus. He called it a logic, but it was a language of pure funcFons --

FinalExamTherewillbeamidtermexam,inmidtermweekWednesday,October26

Therewillbeafinalexam,inexamperiod(January—Makeyourtravelplansaccordingly)

16

Page 17: COS 326 Func,onal Programming: An elegant weapon for the ... · In 1936, Alonzo Church invented the lambda calculus. He called it a logic, but it was a language of pure funcFons --

Assignment0

FigureouthowtodownloadandinstallOCaml4.03onyourmachinebytheFmepreceptbeginstomorrow.

(or,howtouseOCamlbysshtoPrincetonUniversityservers)

ResourcesPage:

hZp://www.cs.princeton.edu/courses/archive/fall15/cos326/resources.php

Hint:

ocaml.org

17

Page 18: COS 326 Func,onal Programming: An elegant weapon for the ... · In 1936, Alonzo Church invented the lambda calculus. He called it a logic, but it was a language of pure funcFons --

PublicServiceAnnouncement

ThePenisMighterthantheKeyboard:AdvantagesofLonghandOverLaptopNoteTaking

PamMueller(PrincetonUniversity)

DanielOppenheimer(UCLA)JournalofPsychologicalScience,June2014,vol25,no6

hZp://pss.sagepub.com/content/25/6/1159.fullkeytype=ref&siteid=sppss&ijkey=CjRAwmrlURGNw

•  YoulearnconceptualtopicsbeZerbytakingnotesbyhand.•  FacebookandWorldofWarcrasdistractyourclassmates.

18