Haskell The essence of functional programming Correspondence to mathematics: • Types play the role of sets • Sets of functions, Sums, Products important • Programs inhabit types, so are like elements of sets • Programs usually have function type, i.e. are functions from input to output • But are really ‘partial’, i.e. can crash, or fail to return an output • But functions remain pure — they map inputs to outputs, so their result does not depend on the state of memory, external events, etc. If you wish to include such dependencies, they must explicitly be inputs to the function Functional programming in practice • Types model objects in the problem domain. • Programming means defining types and writing functions over types. • Computing with functions means evaluation (reduction). • Variables name values and cannot vary. Haskell • Functional programming language • Launched in 1990 • By Paul Hudak, Philip Wadler, Arvind, Brain Boutel, Jon Fairbairn, Joseph Fasel, Kevin Hammond, John Hughes, Thomas Johnsson, Dick Kieburtz, Rishiyur Nikhil, Simon Peyton Jones, Mike Reeve, David Wise, Jonathan Young • Named after logician Haskell Curry (1990-1982) • Photo c/o Wikipedia Haskell is Functional • Functions are first-class, that is, functions are values which can be used in exactly the same ways as any other sort of value. • The meaning of Haskell programs is centered around evaluating expressions rather than executing instructions. Haskell is Pure Haskell expressions are always referentially transparent: • no mutations; everything (variables, data structures …) is immutable • expressions are side-effect free • programs are deterministic - calling the same function with the same arguments results in the same output Haskell is Lazy Expressions are not evaluated until their results are needed. • It is possible to define and work with infinite data structures. • It enables a more compositional programming style • BUT it makes reasoning about time and space usage more difficult! Why we Choose to Teach Haskell Reasoning about correctness – Close to mathematics, pure so can reason compositionally (piece by piece) Well supported – Good compiler errors, documentation Important ideas – Types, functional programming Scientifically important – New language developments often use Haskell (or similar languages) Level Playing Field – Few students get an unfair advantage from familiarity with the language Expressions and evaluation (reduction) Evaluating any Haskell expression works much like a calculator: 42.0 → 42.0 3 + 4 / (1234 – 1) → 3 + 4 / 1233 → 3 + 3.2441200324412004e-3 → 3.0032441200324413 Expressions and evaluation (reduction) (3 + 4) * (5 – 2) → 7 * (5 – 2) → 7 * 3 → 21 Or alternatively: (3 + 4) * (5 – 2) → (3 + 4) * 3 → 7 * 3 → 21 Can even compute 3+4 and 5-2 concurrently (in parallel)! Expressions and evaluation (reduction) double :: Integer –> Integer double n = 2 * n ---------------------------------- double (3 + 1) → double (3 + 1) → 2 * (3 + 1) → 2 * 4 → 8 Applying a function invert :: Picture -> Picture knight :: Picture invert knight :: Picture ♞ invert ♘ Applying a function scale:: Picture -> Integer -> Picture knight :: Picture scale knight 2 :: Picture ♞ scale 2 ♞ Applying a function scale:: Picture -> Integer -> Picture knight :: Picture scale knight knight ♞ scale ♞.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages14 Page
-
File Size-