1 / 12

Introduction to Functional Programming

Introduction to Functional Programming. I wish we could just…program the ether! - Chris Parsons. Or, How I learned to forget about hardware and love the abstractions . What is FP?. Functional programming is one of several programming paradigms .

thoff
Télécharger la présentation

Introduction to Functional Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Functional Programming I wish we could just…program the ether! - Chris Parsons Or, How I learned to forget about hardware and love the abstractions

  2. What is FP? Functional programming is one of several programming paradigms. A paradigm suggests a shift in thinking. Better even than paradigm, we can consider FP as an alternative model of computation as viewed by the programmer. Some other examples: • Procedural (imperative) programming • Object-oriented programming • Logic programming

  3. What is FP? • The FP Model of Computation: the lambda calculus as a model of mathematical functions • We view every computation as applying a function to a set of inputs to get a result, without side effects. So programs are built from functions rather than objects. • Many computations fit this model well…but some don’t. How would you model interaction with a mouse? A network card? I/O?

  4. History Alonzo Church of Princeton develops the lambda calculus as a tool to solve the Decision Problem, publishes results in 1936 Alan Turing’s solution using Universal Machines (now, Turing machines) was published later the same year and cited Church's solution. This occurred while he was a doctoral student in Church's research group at Princeton. The lambda calculus and Turing machines are early Models of Computation, yet predate computers! What we now call the “von Neumann architecture” was described in a paper by John von Neumann in 1945, detailing the design of an early computer, the EDVAC. The high-level language FORTRAN is designed by a team at IBM Research and a compiler produced in 1957. This is the oldest compiled language still in use.The first version adds numeric calculations, IF, GOTO (for rudimentary loops) to assembly code.

  5. History LISP is developed by McCarthy at MIT after “reading the first chapter of the Lambda Calculus book.” He publishes the original idea in 1960 based on work done starting in 1958; Steve Russell develops first LISP interpreter in 1960; Levin and Hart build the first compiler in 1962. John Backus (who led the FORTRAN design team in 1957) describes FP, a functional programming language, in his 1978 Turing Award lecture and paper. The floodgates open…but in Europe!

  6. Characteristics of pure FP • Referential Transparency – an expression's value depends only on the values of its subexpressions, not the order in which they are evaluated • Immutability (no assignment statement!) - no langauge-level concept of data storage • Functions are first-class (have the same rights and responsibilities as, say, variables) • Reliance on software to “smooth over” hardware limitations (for example, see Integer)

  7. How does FP differ from, say, OOP? • Classes exist in Clojure, but not in Erlang(the term class means something rather different) • There are function definitions, and there is encapsulation, but in terms of modules rather than classes • There is a form of polymorphism available • Code is more lightweight; text layout is more important than braces, begin/end pairs, etc.

  8. How does it differ, indeed? ; In Clojure, polymorphism based on methods not classes (defmulti blank? class) (blank? “not implemented yet”) ; return true if String is blank (defmethod blank? String [s] (every? #(Character/isWhiteSpace %) s)) ; return true if List is blank (empty) (defmethod blank? List [l] (empty? l))

  9. Why do we need FP? • Best academic reason: to improve our ability to reason about a program, in order to prove it correct • Best practical reason: to simplify the development of correct concurrent or distributed programs in situations like multicore processors, parallel computers, or cooperating network of computer systems

  10. Industrial Uses of FP Software development in functional languages has traditionally been more common in Europe than in the US. This is changing. Here are some companies who developed non-trivial systems using FP: • Ericsson: Developed the language Erlang for building switching systems for their telephone products (Sweden) • ViaWeb: Developed an electronic shopping system (ViaMall) in Common Lisp that made it easy for stores to develop and deploy e-commerce websites (Massachusetts). Bought out by Yahoo for $1,000,000 • Galois: Consulting services for a particular business model; uses Haskell (Oregon).

  11. More Industrial Uses of FP • Staples: Acquired Rana, an e-commerce personalization stack built using Clojure, in 2013, still hiring Clojure programmers http://careers.staples.com/sanmateo/ • Amazon.com https://lispjobs.wordpress.com/2015/02/ • Walmart Research: of all places, currently turning to Clojure,see https://www.youtube.com/watch?v=av9Xi6CNqq4 • See also the Microsoft Research office in Cambridge, U.K. (research using Haskell)

  12. Course Overview • Study the basic ideas behind functional programming • First-class and higher-order functions • Referential transparency • Immutable data • Static vs. dynamic typing, eager vs. lazy evaluation of functions • First in the context of the impure functional language Clojure, and its application to the problem of concurrent programs running across, say, multiple cores of a microprocessor to speed up a program’s execution • Then in the context of the pure functional language Erlang, its message-passing model, and its application to problems in both concurrent and distributed programming

More Related