Functional Programming

Slides:



Advertisements
Similar presentations
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 14 Functional Programming Languages - The design of the imperative languages is based directly.
Advertisements

1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
Functional Programming Languages
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
ISBN Chapter 15 Functional Programming Languages Mathematical Functions Fundamentals of Functional Programming Languages Introduction to.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
ISBN Chapter 15 Functional Programming Languages.
ISBN Chapter 15 Functional Programming Languages.
(5.1) COEN Functional Languages  Functional programming basics  Atoms and lists; cons  Useful primitive functions  Predicates  Arithmetic functions.
Chapter 15: Functional Programming Languages
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
ISBN Chapter 15 Functional Programming Languages.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
ISBN Chapter 15 Functional Programming Languages.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
ISBN Chapter 15 Functional Programming Languages.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
Chapter Fifteen: Functional Programming Languages Lesson 12.
ISBN Chapter 15 Functional Programming Languages.
CS 363 Comparative Programming Languages Functional Languages: Scheme.
ISBN Chapter 15 Functional Programming Languages.
ISBN Chapter 15 Functional Programming Languages.
ISBN Chapter 15 Functional Programming Languages.
1 Chapter 15 © 2002 by Addison Wesley Longman, Inc Introduction - The design of the imperative languages is based directly on the von Neumann architecture.
ISBN Chapter 15 Functional Programming Languages.
Chapter 15 Functional Programming Languages. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 15 Topics Introduction Mathematical Functions.
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
Functional Programming Part 1. Organization of Programming Languages-Cheng Big Picture u What we’ve learned so far: Imperative Programming Languages 
ISBN Chapter 15 Functional Programming Languages.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
Comparative Programming Languages Functional programming with Lisp/Scheme.
ISBN Chapter 15 Functional Programming Languages.
Functional Programming
Functional Programming
Functional Programming Languages
Functional Programming Languages
Chapter 15 :Functional Programming Languages
Functional Programming Languages
History of Computing – Lisp
Chapter 15 – Functional Programming Languages
(Functional Programming) Reference: R.Sebesta, Chapter 15
Functional Programming Languages
Functional Programming Languages
Fundamentals of Functional Programming Languages
Chapter 15 Functional Programming Languages
Functional Programming Languages
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming Languages
Functional Programming Languages
Functional Programming Languages
Functional Programming Languages
COP4020 Programming Languages
Functional Programming Languages
15.2 Mathematical Functions
Functional Programming Languages
Functional Programming Languages
Chapter 15 Functional Programming 6/1/2019.
Functional Programming Languages
Functional Programming Languages
Presentation transcript:

Functional Programming By P. S. Suryateja Asst. Professor, CSE Vishnu Institute of Technology

Imperative Vs Functional Languages Imperative Languages These languages contain variables Programs written using imperative languages maintains “state” There are side effects Repetition is through iteration Functional Languages Pure functional languages does not have variables There is no “state” in functional programs There are no side effects Repetition is through recursion

Functional Language Examples LISP Scheme ML Haskell OCaml F#

Mathematical Function A mathematical function is a mapping of member in domain set to a member in range set.

Simple Functions Function definitions are written as function name followed by a list of parameters in parentheses. Ex: cube(x) = x * x * x Ex: cube(2) = 2 * 2 * 2 = 8

Simple Functions (cont...) A lambda expression is a method for defining nameless functions. The lambda expression is the function itself, which is nameless. Ex: λ(x)x * x * x The formal computation model for defining function, function application and recursion using lambda expressions is called as lambda calculus. Ex: Application of lambda expression: (λ(x)x * x * x)(2) gives 8

Functional Forms A functional form or higher order function is a function that takes one or more functions as parameters or produces a function as its result, or both. Examples of functional forms: Function composition Apply-to-all

Function Composition A function composition is a functional form which has two functional parameters and yields a function whose value is the first function applied to the result of the second. Notation: h = f o g Example: f(x) = x + 2, g(x) = 3 * x then h is defined as: h(x) = f(g(x)) = (3*x) + 2

Apply-to-all Apply-to-all is a functional form which takes a functional parameters, applies it each value in a list and yields a list or sequence as result. Notation: α(f, (val1, val2, ....)) Ex: h(x) = x * x then α(h, (2, 3, 4)) yields (4, 9, 16)

LISP LISP is the first functional programming language. Developed by John McCarthy at MIT in 1959. Two categories of data items in LISP are: atoms and lists. Atoms are either symbols, in the form of identifiers, or numeric literals. List elements are pairs, where the first part is the data of the element and second part can be a pointer to an atom, a pointer to another element, or the empty list.

LISP (cont...) Lists are specified in LISP by delimiting their elements with parentheses. Ex: (A B C D) is a simple list. (A (B C) D (E (F G))) is a nested list. Lists are internally maintained as linked lists in LISP as shown in next slide.

LISP (cont...)

LISP Interpreter A universal LISP function is a function which can evaluate any other function in LISP. Function calls were specified in a prefix list form originally called Cambridge Polish notation: (function_name arg1 ... argn) For example, if + is a function that takes two or more numeric parameters, then: (+ 5 7) yields 12 (+ 3 4 7 6) yields 20

LISP Interpreter (cont...) The lambda notation was chosen to specify function definitions. (function_name (LAMBDA (arg1 ... argn) expression)) LISP functions specified using the above notation are called as S-expressions or symbolic expressions. An S-expression can be either an atom or a list. McCarthy developed a universal function capable of evaluating any other function. It was named EVAL.

LISP Interpreter (cont...) Later EVAL was used to construct LISP interpreter. LISP’s only notation was S-expressions. Scoping used in LISP is dynamic scoping.

Scheme Scheme is a dialect of LISP, developed at MIT by Sussman and Steele in 1970s. Scheme is very small in size and is popular in academic circles. Scheme is type less. Scheme uses static scoping.

Scheme : Primitive Numeric Functions Scheme includes primitive functions for the basic arithmetic operations like +, -, *, /. Ex: Expression Value 42 42 (* 3 7) 21 (+ 5 7 8) 20 (- 5 6) -1 (-15 7 2) 6 (-24 (* 4 3)) 12 Other numeric functions in Scheme are: MODULO, ROUND, MAX, MIN, LOG, SIN and SQRT.

Scheme : Defining Functions A Scheme program is a collection of function definitions. In Scheme, a nameless function is defined using the LAMBDA word: (LAMBDA (x) (* x x)) The above function can be applied as: ((LAMBDA (x) (* x x)) 7) yields 49

Scheme : Defining Functions (cont...) Scheme’s special function DEFINE serves two fundamental needs: To bind a name to a value To bind a name to a lambda expression Examples of DEFINE to bind a name to a value: (DEFINE pi 3.14159) (DEFINE two_pi (* 2 pi))

Scheme : Defining Functions (cont...) Syntax of DEFINE to bind a name to a lambda expression is: (DEFINE (function_name params) (expression)) Ex: (DEFINE (square number) (* number number)) (square 5) yields 25

Scheme : Numeric Predicate Functions A predicate function is one that returns a Boolean value. Some predicate functions in Scheme: Function Meaning = Equal < > Not equal > Greater than < Less than >= Greater than or equal to <= Less than or equal to EVEN? Is it an even number? ODD? Is it an odd number? ZERO? Is it zero? Boolean values in Scheme are #T and #F (or #t and #f).

Scheme : Control Flow Scheme uses three different constructs for control flow: IF COND Recursion The syntax of IF selector function is: (IF predicate the_expr else_expr) Ex: (DEFINE (factorial n) (IF (<= n 1) 1 (* n (factorial (- n 1))) ))

Scheme : Control Flow (cont...) Example on COND selector: (DEFINE (leap? year) (COND ((ZERO? (MODULO year 400)) #T) ((ZERO? (MODULO year 100)) #F) (ELSE (ZERO? (MODULO year 4))) ))

Scheme : List Functions The primitive function QUOTE returns the parameter without change. Ex: (QUOTE A) returns A (QUOTE (A B C)) returns (A B C) The CAR function accepts a list and returns the first element in the list. Ex: (CAR ‘(A B C)) returns A (CAR ‘((A B) C D)) returns (A B) The CDR functions accepts a list and returns remaining elements in the list except the first element. Ex: (CDR ‘(A B C)) returns (B C) (CDR ‘((A B) C D)) returns (C D)

Scheme : List Functions (cont...) The CONS function accepts two parameters and creates a list of those parameters. Ex: (CONS ‘A ‘(B C)) returns (A B C) (CONS ‘(A B) ‘(C D)) returns ((A B) C D) The LIST accepts multiple parameters and creates a list of those parameters. Ex: (LIST ‘apple ‘orange ‘grape) returns (apple orange grape)

Scheme : Predicate Functions for Symbolic Atoms and Lists The EQ? function takes two atoms as parameters and #T if they point to same memory location. Otherwise, it returns #F. Ex: (EQ? ‘A ‘A) returns #T (EQ? ‘A ‘B) returns #F

Scheme : Predicate Functions for Symbolic Atoms and Lists (cont...) The EQV? function takes two atoms as parameters and compares the content instead of pointers. It #T if they are same or #F if they are not same. Ex: (EQV? ‘A ‘A) returns #T (EQV? 3.4 (+ 3 0.4)) returns #T (EQV? 3.0 3) returns #F

Scheme : Predicate Functions for Symbolic Atoms and Lists (cont...) The LIST? function returns #T if its single argument is a list and #F otherwise. Ex: (LIST? ‘(X Y)) returns #T (LIST? ‘X) returns #F

Scheme : LET LET is a function used to create a local scope in which names are temporarily bound to the values of expressions. LET is often used to factor out common sub-expressions from more complicated expressions. LET is a shorthand for a LAMBDA expression applied to a parameter. Ex: (LET ((alpha 7)) (* 5 alpha)) ((LAMBDA (alpha) (* 5 alpha)) 7)

Meta Language [ML] ML is a static scoped language like Scheme. ML was developed by Milner in 1990. Differences between ML and Scheme are: ML is a strongly typed language. ML uses syntax that is close to imperative language. A table called evaluation environment stores the names of all declared identifiers in a program along with their types.

ML (cont...) Function declaration in ML: fun function_name(params) = expression; Ex: fun circum(r) = 3.1415 * r * r; fun times10(x) = 10 * x; fun square(x) = x * x; Function call: square(2.75);

ML (cont...) Types can be specified as follows: fun square(x : real) = x * x; fun square(x) = (x : real) * x; fun square(x) = x * (x : real);

ML (cont...) The if statement in ML is as follows: if expression then then_expr else else_expr Ex: fun fact(n : int) : int = if n <= 1 then 1 else n * fact (n – 1);

ML (cont...) ML supports lists and list operations. The hd, tl and :: are ML’s versions of Scheme’s CAR, CDR and CONS. Names are bound to values with value declaration statements of the form: val new_name = expression; Ex: val distance = time * speed;

ML (cont...) Normal use of val is in let expression: let val radius = 2.7; val pi = 3.14159; in pi * radius * radius; end;

ML (cont...) The map function takes a single parameter, which is a function. The resulting function takes a list as a parameter and applies the function to each element in the list. Ex: fun cube x = x * x * x; val cubeList = map cube; val newList = cubeList [1, 3, 5];