Functional

Functional - COP4020 Programming Languages Functional...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
COP4020 Programming Languages Functional Programming Prof. Robert van Engelen
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COP4020 Spring 2011 2 1/13/11 Overview ! What is functional programming? ! Historical origins of functional programming ! Functional programming today ! Concepts of functional programming ! Functional programming with Scheme ! Learn (more) by example
Background image of page 2
COP4020 Spring 2011 3 1/13/11 What is Functional Programming? ! Functional programming is a declarative programming style (programming paradigm) " Pro: flow of computation is declarative, i.e. more implicit " Pro: promotes building more complex functions from other functions that serve as building blocks (component reuse) " Pro: behavior of functions defined by the values of input arguments only (no side-effects via global/static variables) " Cons: function composition is (considered to be) stateless " Cons: programmers prefer imperative programming constructs such as statement sequencing, while functional languages emphasize function composition
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COP4020 Spring 2011 4 1/13/11 Concepts of Functional Programming ! Pure functional programming defines the outputs of a program purely as a function of the inputs with no notion of internal state (no side effects) " A pure function can be counted on to return the same output each time we invoke it with the same input parameter values " No global (statically allocated) variables " No explicit (pointer) assignments ! Dangling pointers and un-initialized variables cannot occur! " Example pure functional programming languages: Miranda, Haskell, and Sisal ! Non-pure functional programming languages include “imperative features” that cause side effects (e.g. destructive assignments to global variables or assignments/changes to lists and data structures) " Example: Lisp, Scheme, and ML
Background image of page 4
COP4020 Spring 2011 5 1/13/11 Functional Language Constructs ! Building blocks are functions ! No statement composition " Function composition ! No variable assignments " But: can use local “variables” to hold a value assigned once ! No loops " Recursion " List comprehensions in Miranda and Haskell " But: “do-loops” in Scheme ! Conditional flow with if-then-else or argument patterns ! Functional languages are statically (Haskell) or dynamically (Lisp) typed ! Haskell examples: gcd a b | a == b = a | a > b = gcd (a-b) b | a < b = gcd a (b-a) fac 0 = 1 fac n = n * fac (n-1) member x [] = false member x (y:xs) | x == y = true | x <> y = member x xs
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COP4020 Spring 2011 6 1/13/11 Theory and Origin of Functional Languages ! Church's thesis: " All models of computation are equally powerful " Turing's model of computation: Turing machine ! Reading/writing of values on an infinite tape by a finite state machine " Church's model of computation: Lambda Calculus " Functional programming languages implement Lambda Calculus ! Computability theory " A program can be viewed as a constructive proof that some mathematical object with a desired property exists " A function is a mapping from inputs to output objects and computes output objects from appropriate inputs !
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/01/2012 for the course COP 4020 taught by Professor Engelen during the Spring '11 term at FSU.

Page1 / 42

Functional - COP4020 Programming Languages Functional...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online