270 likes | 419 Views
Functional Programming GC16 / 3011. Chris Clack. Functional Programming. This Lecture: Introduction Simple example Haiku or Karate? (industrial example) The Course. Introduction. How many programming languages do you think exist?
E N D
Functional Programming GC16 / 3011 Chris Clack
Functional Programming • This Lecture: • Introduction • Simple example • Haiku or Karate? (industrial example) • The Course
Introduction • How many programming languages do you think exist? • Java, C++, Cobol, Fortran, Basic, APL, C, Pascal, Haskell, Miranda, Prolog, LISP… • How many languages can you use? • Are all these other languages just “junk”? • Why can’t you use Java for everything?
Introduction • What’s COOL about HOT languages? • What’s NEAT about GARBAGE? • How can a LAZY language improve performance? • Some languages support “infinite” data structures…(…how do they do that?)
Introduction SML Miranda Haskell • Imperative versus Declarative • Functional versus Logic • “instructing a machine” versus “solving a problem” Java C++ FORTRAN LISP Prolog
Simple Example • Assume “results” is the name of a variable that contains a sequence of 100 integers; • write code to select all those less than 10
Simple Example - imperative int small[100]; for (j=0,k=0; j<100; j++){ if (results[j]<10) { small[k]=results[j]; k++; } } return(small);
Simple Example - functional filter (< 10) results
Haiku or Karate? • FP languages are renowned for their elegance • But - are they like Japanese “Haiku” poetry? • (elegant but not very practical) • Or - are they like Karate? • (elegant and useful in a fight)
Haiku or Karate? Prototyping O-O DesignsUsing FP Techniques
Haiku or Karate? – the problem • Commercial Constraints • The world’s largest IT Consultancy • An international Settlement Bank • A “mission-critical” financial system • Over 100 programmers • C++ required by client
Haiku or Karate? – the problem • The System: A network of components
Haiku or Karate? – the problem • Requirements: • Discrete-event simulation of component network • Prototyping of central (optimisation and approximation) algorithms
Haiku or Karate? – constraints • C++ “not viable” for prototyping/simulation • take too long to develop underlying components • rapid-prototyping O-O language (e.g. Smalltalk) “not desirable” • raises question about overall suitability of C++ • Alternative approach: use a Functional Language (“Miranda”) • HOT, LAZY, with GARBAGE COLLECTION • NO POINTERS. NO ASSIGNMENT
Haiku or Karate? – selling points • Speed and Clarity with which algorithms can be: • expressed • validated • Can simulate key O-O designs in detail • with minimal detail for other components • Access to expertise: • a “champion”
Haiku or Karate? – selling points • Note: Speed of execution was almost totally irrelevant!
c1 c2 c9 a b c3 c4 c8 c6 c5 c d c7 Haiku or Karate? - techniques • Modelling the component network file1 file2
Haiku or Karate? - techniques c1 = read “file1” c5 = read “file2” c9 = b (c2, c6, c8) (c2, c3) = a (c1, c4) (c4, c6) = c (c5, c7) (c7, c8) = d c3 • Recursive (looping) functions a, b, c and d • c1, c2 etc. are potentially-infinite streams of (time, value) events
Haiku or Karate? - techniques • Simulating behaviour • simple behavioural executable specification • expression-based • synthetic (statistical) data generation • Could also use Miranda algorithms as specification for subsequent implementation in C++
Haiku or Karate? - results • Rapid Development • About five times faster than C++ • Concise Expression • 6 pages of Miranda = about 25 pages of C++ • Simulation and specification of complex processes • design optimised early in lifecycle • confidence increased through validation on real data
Haiku or Karate? - results • Almost NO errors in prototype code • Vast reduction in errors in final C++ code • Viewed as a commercial advantage • Promoted within Andersen Consulting - part of “Method/1” • “champion” now promoted to Manager
The Course • Focus: • Fab Features • Ingenious Implementation • Programming Skills • Add another language/paradigm to your CV • Vehicle: • A functional language “Miranda” • Elegant implementation technology • Extends to other languages (e.g. Java) • Coursework: 10% (1 coursework, may be MCT)
The Course - Lectures • LECTURES: • Monday 12-1pm 25 Gordon Square 107 • Tuesday 9-10am MPEB 1.02 • Friday 11-12noon Roberts 110 • PRACTICALS (TBA: if/when I have a demonstrator!): • Friday 4-5pm MPEB 1.21 • Tuesday 9-10am DOUBLE BOOKED • mostly for GC16, less relevant for 3011 • Demonstrator – TBA
The Course - Topics • “Lambda Calculus” - a simple introduction • Introduction to Miranda • developing some programming skills • developing problem-solving (thinking) skills • Introduction to implementation techniques • graph reduction • memory management (cf. C, C++, algorithms) • garbage collection (cf. Java)
The Course - Books • Programming with Miranda, Clack, Myers and Poon, Prentice-Hall 1994 ISBN 0-13-192592-X. • Research Directions in Parallel Functional Programming, Hammond and Michaelson (Eds.), Springer, ISBN 1-85233-092-9, 1999 • The Implementation of Functional Programming Languages, Peyton-Jones, Prentice-Hall 1986 • Garbage Collection: Algorithms for Automatic Dynamic Memory Management, Jones and Lins, Wiley 1996 ISBN 0 471 94148 4
The Course - Communication • 3011@cs.ucl.ac.uk (3011-request@cs.ucl.ac.uk) • gc16@cs.ucl.ac.uk (gc16-request@cs.ucl.ac.uk) • http://www.cs.ucl.ac.uk/teaching/3c11 • http://www.cs.ucl.ac.uk/teaching/D16