1 / 20

Concepts from Functional Programming Languages

Concepts from Functional Programming Languages. Peter Gorm Larsen. Agenda. Introduction to the Functional Programming Paradigm The notion of higher order functions Polymorphic examples of standard higher order functions. Introduction to FP.

Download Presentation

Concepts from Functional Programming Languages

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. Concepts from Functional Programming Languages Peter Gorm Larsen Functional Programming Language Concepts

  2. Agenda • Introduction to the Functional Programming Paradigm • The notion of higher order functions • Polymorphic examples of standard higher order functions Functional Programming Language Concepts

  3. Introduction to FP • The design of the imperative languages is based directly on the von Neumann architecture • Efficiency is the primary concern, rather than the suitability of the language for software development • The design of the functional languages is based on mathematical functions • A solid theoretical basis that is also closer to the user, but relatively unconcerned with the architecture of the machines on which programs will run Functional Programming Language Concepts

  4. Principles of FP • Treats computation as evaluation of mathematical functions (and avoids state) • Data and programs are represented in the same way • Functions as first-class values – Higher-order functions: functions that operate on, or create, other functions – Functions as components of data structures • Lambda calculus provides a theoretical framework for describing functions and their evaluation • It is a mathematical abstraction rather than an imperative programming language Functional Programming Language Concepts

  5. History • lambda calculus (Church, 1932) • simply typed lambda calculus (Church, 1940) • lambda calculus as prog. lang. (McCarthy(?), 1960, Landin 1965) • polymorphic types (Girard, Reynolds, early 70s) • algebraic types ( Burstall & Landin, 1969) • type inference (Hindley, 1969, Milner, mid 70s) • lazy evaluation (Wadsworth, early 70s) • Equational definitions Miranda 80s • Type classes Haskell 1990s • Microsoft F# etc 2000s Functional Programming Language Concepts

  6. Varieties of FP languages • Typed (ML, Haskell) vs untyped (Scheme, Erlang) • Pure vs Impure • impure have state and imperative features • pure have no side effects, “referential transparency” • Strict vs Lazy evaluation Functional Programming Language Concepts

  7. Declarative style of programming • Declarative Style of programming - emphasis is placed on describing what a program should do rather than prescribing how it should do it. • Functional programming - good illustration of the declarative style of programming. • A program is viewed as a function from input to output. • Logic programming – another paradigm • A program is viewed as a collection of logical rules and facts (a knowledge-based system). Using logical reasoning, the computer system can derive new facts from existing ones. Functional Programming Language Concepts

  8. Functional style of programming • A computing system is viewed as a function which takes input and delivers output. • The function transforms the input into output . • Functions are the basic building blocks from which programs are constructed. • The definition of each function specifies what the function does. • It describes the relationship between the input and the output of the function. Functional Programming Language Concepts

  9. Agenda • Introduction to the Functional Programming Paradigm • The notion of higher order functions • Polymorphic examples of standard higher order functions Functional Programming Language Concepts

  10. First-Class Functions Data values are first-class if they can • be assigned to local variables • be components of data structures • be passed as arguments to functions • be returned from functions • be created at run-time Functional Programming Language Concepts

  11. Higher-order Functions • Every function has an order: • A function that does not take any functions as parameters, and does not return a function value, has order 1 • A function that takes a function as a parameter or returns a function value has order n+1, where n is the order of its highest-order parameter or returned value • A small example: Twice: (int -> int) * int -> int Twice(f,x) == f( f (x)) • Or TwiceCur: (int -> int)-> int -> int TwiceCur(f)(x) == f( f (x)) Functional Programming Language Concepts

  12. Functions in Programming Languages • How functions are treated by programming languages? Functional Programming Language Concepts

  13. Nested Functions and Closures • Return a function from function call function f(x) { var y = x; return function (z){y += z; return y;} } var h = f(5); h(3); • In order to handle this one needs to introduce closures • A closure is a function that captures the bindings of free variables in its lexical context. Functional Programming Language Concepts

  14. Agenda • Introduction to the Functional Programming Paradigm • The notion of higher order functions • Polymorphic examples of standard higher order functions Functional Programming Language Concepts

  15. Predefined Higher-Order Functions in Functional Languages • We will use three important predefined higher-order functions: • map • filter • foldr • foldl • Actually, foldr and foldl are very similar, as you might guess from the names Functional Programming Language Concepts

  16. The Map Function • Map applies a function to every element of a list: Map[@A,@B]: (@A -> @B) -> seq of @A -> seq of @B Map(f)(list) == [f(list(i)) | i in set inds list] Functional Programming Language Concepts

  17. The Filter Function • Filter selects every element that satisfies a predicate: Filter[@A]: (@A -> bool) -> seq of @A -> seq of @A Filter(pred)(list) == [list(i) | i in set inds list & pred(list(i))] Functional Programming Language Concepts

  18. The FoldR Function • Folds all elements in a list from the right into one value (a simple pattern of recursion): FoldR[@A,@B]: (@A * @B -> @B) -> @B -> seq of @A -> @B FoldR(f)(neutral)(list) == if list = [] then neutral else f(hd list,FoldR(f)(neutral)(tl list)) Example usage: Sum = FoldR(+)(0) Product = FoldR(*)(1) Or = FoldR(or)(false) And = FoldR(and)(true) Functional Programming Language Concepts

  19. Summary • What have I presented today? • Introduction to the Functional Programming Paradigm • The notion of higher order functions • Polymorphic examples of standard higher order functions • What do you need to do now? • Complete your distributed real time model for your project Functional Programming Language Concepts

  20. Quote of the day Program designers have a tendency to think of the users as idiots who need to be controlled. They should rather think of their program as a servant, whose master, the user, should be able to control it. If designers and programmers think about the apparent mental qualities that their programs will have, they'll create programs that are easier and pleasanter — more humane — to deal with. John McCarthy Functional Programming Language Concepts

More Related