Functional Programming Languages - PowerPoint PPT Presentation

1 / 19
About This Presentation
Title:

Functional Programming Languages

Description:

... primary concern, rather than the suitability of the language for software development. The imperative languages represent a progression of developments to ... – PowerPoint PPT presentation

Number of Views:112
Avg rating:3.0/5.0
Slides: 20
Provided by: wandak4
Category:

less

Transcript and Presenter's Notes

Title: Functional Programming Languages


1
Chapter 15
  • Functional Programming Languages

2
Introduction
  • 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 imperative languages represent a progression
    of developments to improve the basic model,
    Fortran I

3
Introduction
  • 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
  • LISP is the most important and most widely used
    of the functional languages

4
Mathematical Functions
  • Defined
  • A mathematical function is a mapping of members
    of one set, called the domain set, to another
    set, called the range set
  • Characteristics
  • The evaluation order of their mapping expressions
    is controlled by recursion and conditional
    expressions, instead of by the sequencing and
    iterative repetition that are common to the
    imperative programming languages
  • A mathematical function defines a value, instead
    of specifying a sequence of operations on values
    in memory to produce a value thus, there are no
    side effects connected to variables that
    represent memory locations

5
Simple Functions
  • Function definitions
  • Frequently written as a function name, followed
    by a list of parameters in parentheses, followed
    by the mapping expression
  • For examplecube(x) ? x ? x ? x, where x is a
    real number

6
Simple Functions
  • Lambda expression
  • Describes a nameless function
  • Specifies the parameter(s) and the mapping of a
    function
  • The lambda expression is the function itself
  • For example?(x) x ? x ? x is the lambda
    expression for the function cube(x) ? x ? x ? x

7
Simple Functions
  • Lambda expression
  • A lambda expression is applied to one or more
    parameters by placing the parameter(s) after the
    expression
  • For example(?(x) x ? x ? x) (3)which
    evaluates to 27

8
Functional Forms
  • Defined
  • A higher-order function, or functional form, is
    one that either takes functions as parameters or
    yields a function as its result, or both
  • Representative types
  • Function Composition
  • A functional form that takes two functions as
    parameters and yields a function whose result is
    a function whose value is the first actual
    parameter function applied to the result of the
    application of the second
  • Form h ? f ? gwhich means h(x) ? f(g(x))

9
Functional Forms
  • Representative types
  • Construction
  • A functional form that takes a list of functions
    as parameters and yields a list of the results of
    applying each of its parameter functions to a
    given parameter
  • Form f, gFor f(x) ? x ? x ? x and g(x) ? x
    3,f, g (4) yields (64, 7)

10
Functional Forms
  • Representative types
  • Apply-to-all
  • A functional form that takes a single function as
    a parameter and yields a list of values obtained
    by applying the given function to each element of
    a list of parameters
  • Form ?For h(x) ? x ? x ? x, ? (h, (3, 2, 4))
    yields (27, 8, 64)

11
Fundamentals of Functional Programming Languages
  • The objective of the design of a FPL is to mimic
    mathematical functions to the greatest extent
    possible
  • The basic process of computation is fundamentally
    different in a FPL than in an imperative language
  • In an imperative language, operations are done
    and the results are stored in variables for later
    use
  • Management of variables is a constant concern and
    source of complexity for imperative programming
  • In a FPL, variables are not necessary

12
Fundamentals of Functional Programming Languages
  • In a FPL, the evaluation of a function always
    produces the same result given the same
    parameters
  • This is called referential transparency
  • Referential transparency simplifies the semantics
    of functional languages
  • Well-defined functional languages require only a
    small number of primitive functions from which
    more complex functions can be constructed

13
The First Functional Programming Language LISP
  • LISP was the first functional programming
    language
  • Originally, LISP was a typeless language
  • There were only two data types
  • Atom
  • Literal atom (symbol)
  • Numeric atom (number)
  • List
  • Linked list
  • Property list (represented as a special case of a
    linked list)

14
The First Functional Programming Language LISP
  • Internal representation of a LISP list
  • Stored as a single-linked list
  • Each node has two pointers and represents an
    element
  • A node for an atom has its first pointer pointing
    to some representation of the atom, i.e., its
    symbol or numeric value
  • A node for a sublist element has its first
    pointer pointing to the first node of the sublist
  • In both cases, the second pointer of a node
    points to the next element of the list
  • A list is referenced by a pointer to its first
    element

15
The First Functional Programming Language LISP
  • LISP lists are specified by delimiting their
    elements within parentheses
  • Sample lists
  • (A B C D)
  • Each element is an atom
  • (A (B C) D (E (F G)))
  • Element 1 is the atom A
  • Element 2 is the sublist (B C)
  • Element 3 is the atom D
  • Element 4 is the sublist (E (F G)), which has a
    the sublist (F G) as its second element

16
The First Functional Programming Language LISP
  • Internal representation of the two sample lists

17
The First Functional Programming Language LISP
  • Lambda notation is used to specify function
    definitions (see slides 6 7)
  • Program code (function applications) and data
    have the same form in LISP parenthesized lists
  • Consider the list (A B C D) . . .
  • If interpreted as data, it is a list of 4
    elements (atoms)
  • If interpreted as code, it is the application
    of the function named A to the 3 parameters B, C,
    and D

18
The First Functional Programming Language LISP
  • Lets look at a simple LISP program . . .

19
Hello World LISP Style(corrected and
simplified)
gt Define a function Greeting gt (defun
Greeting (hello i) (cond ((eq i 2) (print "Hello
World")) (T (print "Goodbye World")))) gt Call
the function with 2 parameters gt (Greeting
hello 2) Prints "Hello World"
"Hello World"
Write a Comment
User Comments (0)
About PowerShow.com