Programming Languages winter term 2010/11 Jörg Kreiker, I2+I7
Getting started What is your favorite PL? Do you know any functional language? Logic programming? Expectations? Implement the joke in your favorite PL
Popular languages: langpop.com
Most talked about languages
Meet Haskell: a straight bat Download interpreter (hugs/ghci) and compiler (ghc) from haskell.org Pure, lazy, functional language Use declarative languages only As opposed to imperative Freely available books and tutorials Printing 500 lines in Haskell
Haskell.org
realworldhaskell.org
Organization Lectures: Thursday, 12 noon – 1.30pm, 00.08.038 Mostly theory Tutorials: Thursday, 2pm-3.30pm, tbd First tutorial: next week Mostly programming Online problem solving Discussion and comparison of languages and paradigms Website: www2.in.tum.de/hp/Main?nid=142
Grading Written exam, 40 points, 90 minutes Programming project “Exen” Implement a Haskell interpreter in Haskell Earns you up to 4 bonus points Due before xmas “Exen” Small tests during tutorials Earn you 1 (>50%) or 2 (>75%) bonus points
Code of conduct Interaction, interaction, interaction Buzz votes and polls Ask questions (English or German) No cell phones, no eating 1 or 2 breaks of 5-10 minutes per lecture Laptops for online programming Coffee is fine
http://www.cs.brown.edu/~sk/Publications/Books/ProgLangs/
Learning goals
1 To write short error-free programs, without testing, for tasks such as binary search, and to use a formal system such as Hoare logic to verify them.
2 To use language features such as higher-order functions and continuations to implement tasks such as backtracking and memoization.
Some modern features and techniques Continuations Polymorphism First-class functions (closures, lambda) Pattern matching Software transactional memory Memoization and backtracking Exceptions Monads
3 To write concurrent programs using shared variables or message-passing, and to give rigorous informal arguments for their lack of errors.
4 To specify precisely and implement correctly a simple programming language.
5 To estimate the time and space requirements of basic operations on widely used data representations.
6 To write a clear and error-free program of sufficient complexity – say a syntax-directed editor – that it must be structured carefully.
Content Haskell Prolog Laziness State I/O Continuations Lambda calculus Types and semantics Prolog Programming by search Backtracking
Clarify and define formally… Syntax vs semantics Types Scope and binding Functions vs procedures Call-by-value, call-by name, call-by need Identifiers vs variables State Modularity Polymorphism
The factorial function П fac(n) = i=1 n i implement the factorial function in your favorite language discuss with your neighbor 3 minutes
Haskell freshman fac n = if n == 0 then 1 else n * fac (n-1)
Haskell sophomore fac = \n -> if (==) n 0 then 1 else (*) n (fac ((-) n 1))
Haskell junior fac 0 = 1 fac n = n * fac (n-1)
Haskell senior fac n = foldr (*) [1..n] fac n = foldl (*) [1..n]
Memoizing Haskell programmer facs n = scanl (*) 1 [1..] fac n = facs !! n
Continuation-passing style facCPS k 0 = k 1 facCPS k n = facCPS (k . (n *)) (n-1) fac = facCPS id
Accumulating programmer facACC a 0 = a facACC a n = fac (a*n) (n-1) fac = facACC 1
Haskell boy scout y f = f (y f) fac = y (\f n -> if n==0 then 1 else n * f (n-1))
Pure madness s f g x = f x (g x) k x y = x b f g x = f (g x) c f g x = f x g y f = f (y f) cond p f g x = if p x then f x else g x fac = y (b (cond ((==) 0) (k 1)) (b (s (*)) (c b pred)))
http://www.willamette.edu/~fruehr/haskell/evolution.html
Conclusion Learning goals, organization, content Haskell primer Currying Pattern matching List combinators (foldr, foldl, scanl) Iteration vs recursion Accumulators and tail calls Continuations S, K, B, C, Y combinators
Outlook Next time: more on Haskell Lists and trees Types More language features