Session 12

Functional Programming Language

January 6th, 2018

So, What is Functional Programming Language ?

functional programming is a programming paradigm—a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data.

  • The design of the imperative languages is based directly on the von Neumann architecture
  • The design of the functional languages is based on mathematical functions

Mathematical Functions

  • A mathematical function is a mapping of members of one set, called the domain set, to another set, called the range set
  • A lambda expression specifies the parameter(s) and the mapping of a function in the following form

l(x) x * x * x

for the function  cube(x) = x * x * x

 

Lambda Expressions

  • Lambda expressions describe nameless functions
  • Lambda expressions are applied to parameter(s) by placing the parameter(s) after the expression

e.g.,   (l(x) x * x * x)(2)

which evaluates to 8

 

Functional Forms

  • A higher-order function, or functional form, is one that either takes functions as parameters or yields a function as its result, or both

Function Composition

A functional form that takes two functions as parameters and yields a function whose value is the first actual parameter function applied to the application of the second

Form: h º f ° g

which means h (x) º f ( g ( x))

For f (x) º x + 2  and  g (x) º 3 * x,

h º f ° g yields (3 * x)+ 2

 

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: a

For h(x) º x * x

a(h, (2, 3, 4))  yields  (4, 9, 16)

 

LISP Data Types and Structures

  • Data object types: originally only atoms and lists
  • List form: parenthesized collections of sublists and/or atoms

e.g., (A B (C D) E)

  • Originally, LISP was a typeless language
  • LISP lists are stored internally as single-linked lists

LISP Interpretation

  • Lambda notation is used to specify functions and function definitions. Function applications and data have the same form.
  • The first LISP interpreter appeared only as a demonstration of the universality of the computational capabilities of the notation

Primitive Functions & LAMBDA Expressions

  • Primitive Arithmetic Functions: +, -, *, /, ABS, SQRT, REMAINDER, MIN, MAX

e.g., (+ 5 2) yields 7

  • Lambda Expressions

–Form is based on l notation

e.g., (LAMBDA (x) (* x x)

x is called a bound variable

  • Lambda expressions can be applied to parameters

e.g., ((LAMBDA (x) (* x x)) 7)

  • LAMBDA expressions can have any number of parameters

(LAMBDA (a b x) (+ (* a x x) (* b x)))

 

Special form functinon : DEFINE

DEFINE – Two forms:

1.To bind a symbol to an expression

e.g., (DEFINE pi 3.141593)

Example use: (DEFINE two_pi (* 2 pi))

These symbols are not variables – they are like the names bound by Java’s final declarations

2.To bind names to lambda expressions (LAMBDA is implicit)

e.g., (DEFINE (square x) (* x x))

Example use: (square 5)

– The evaluation process for DEFINE is different! The first parameter is never evaluated. The second parameter is evaluated and bound to the first parameter.

 

Numeric Predicate Functions

  • #T (or #t) is true and #F (or #f) is false (sometimes () is used for false)
  • =, <>, >, <, >=, <=
  • EVEN?, ODD?, ZERO?, NEGATIVE?
  • The NOT function inverts the logic of a Boolean expression

Control Flow

  • Selection- the special form, IF

  (IF predicate then_exp else_exp)

    (IF (<> count 0)

(/ sum count)

)

  • Recall from Chapter 8 the COND function:

(DEFINE (leap? year)

(COND

((ZERO? (MODULO year 400)) #T)

((ZERO? (MODULO year 100)) #F)

(ELSE (ZERO? (MODULO year 4)))

))

 

List Functions

  • QUOTE – takes one parameter; returns the parameter without evaluation

–QUOTE is required because the Scheme interpreter, named EVAL, always evaluates parameters to function applications before applying the function.  QUOTE is used to avoid parameter evaluation when it is not appropriate

–QUOTE can be abbreviated with the apostrophe prefix operator

‘(A B) is equivalent to (QUOTE (A B))

 

Predicate Function: EQ?

  • EQ? takes two expressions as parameters (usually two atoms); it returns #T if both parameters have the same pointer value; otherwise #F

(EQ? ‘A ‘A) yields #T

(EQ? ‘A ‘B) yields #F

(EQ? ‘A ‘(A B)) yields #F

(EQ? ‘(A B) ‘(A B)) yields #T or #F

(EQ? 3.4 (+ 3 0.4))) yields #T or #F

 

Predicate Function: EQV?

  • EQV? is like EQ?, except that it works for both symbolic and numeric atoms; it is a value comparison, not a pointer comparison

(EQV? 3 3) yields #T

(EQV? ‘A 3) yields #F

(EQV 3.4 (+ 3 0.4)) yields #T

(EQV? 3.0 3) yields #F  (floats and integers are different)

Predicate Functions: LIST? and NULL?

  • LIST? takes one parameter; it returns #T if the parameter is a list; otherwise #F

(LIST? ‘()) yields #T

  • NULL? takes one parameter; it returns #T if the parameter is the empty list; otherwise #F

(NULL? ‘(())) yields #F

 

Tail Recursion in Scheme

  • Definition: A function is tail recursive if its recursive call is the last operation in the function
  • A tail recursive function can be automatically converted by a compiler to use iteration, making it faster
  • Scheme language definition requires that Scheme language systems convert all tail recursive functions to use iteration

Functions That Build Code

  • It is possible in Scheme to define a function that builds Scheme code and requests its interpretation
  • This is possible because the interpreter is a user-available function, EVAL

Comparing Functional and Imperative Languages

  • Imperative Languages:

–Efficient execution

–Complex semantics

–Complex syntax

–Concurrency is programmer designed

  • Functional Languages:

–Simple semantics

–Simple syntax

–Less efficient execution

–Programs can automatically be made concurrent

This entry was posted in Session Summary. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *