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.
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.
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.
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
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.
ISBN Chapter 15 Functional Programming Languages.
ISBN Chapter 15 Functional Programming Languages.
Dr. Muhammed Al-Mulhem ICS An Introduction to Functional Programming.
Imperative Programming
Haskell Chapter 1, Part I. Highly Recommended  Learn you a Haskell for Great Good. Miran Lipovaca.
The College of Saint Rose CIS 433 – Programming Languages David Goldschmidt, Ph.D. from Concepts of Programming Languages, 9th edition by Robert W. Sebesta,
Functional Languages. Why? Referential Transparency Functions as first class objects Higher level of abstraction Potential for parallel execution.
1 Functional Programming In Text: Chapter Chapter 2: Evolution of the Major Programming Languages Outline Functional programming (FP) basics A bit.
 The design of the imperative languages is based directly on the von Neumann architecture  Efficiency is the primary concern  Low-level specifications:
ISBN Chapter 15 Functional Programming Languages.
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
ISBN Chapter 15 Functional Programming Languages.
1 Programming Languages and Paradigms Functional Programming.
CHAPTER 15 & 16 Functional & Logic Programming Languages.
Chapter Fifteen: Functional Programming Languages Lesson 12.
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.
TIVDM2Functional Programming Language Concepts 1 Concepts from Functional Programming Languages Peter Gorm Larsen.
1-1 An Introduction to Functional Programming Sept
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
1 Scheme (Section 11.2) CSCI 431 Programming Languages Fall 2003.
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.
Programming Languages 2nd edition Tucker and Noonan
Chapter 2: Lambda Calculus
Functional Programming
Functional Programming Languages
Functional Programming Languages
Functional Programming Languages
Conditional Expressions
Functional Programming
History of Computing – Lisp
CS 3304 Comparative Languages
Unit – 3 :LAMBDA CALCULUS AND FUNCTIONAL PROGRAMMING
Carlos Varela Rennselaer Polytechnic Institute September 5, 2017
A lightening tour in 45 minutes
Functional Programming
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Functional Programming Languages
Introduction to Functional Programming in Racket
Fundamentals of Functional Programming Languages
Mini Language Interpreter Programming Languages (CS 550)
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming Languages
Announcements Quiz 6 HW7 due Tuesday, October 30
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
Functional Programming Languages
Introduction to Functional Programming in Racket
CSE S. Tanimoto Lambda Calculus
15.2 Mathematical Functions
Chapter 15 Functional Programming 6/1/2019.
Functional Programming Languages
Carlos Varela Rennselaer Polytechnic Institute September 6, 2019
Presentation transcript:

Functional Programming Yoonsik Cheon ycheon@utep.edu CS 3360 Chapter 15 Spring 2011

Objectives Become familiar with the functional programming paradigm Know its foundation Understand how to write programs in Haskell

Outline Imperative vs. functional programming Functions and lambda calculus

Imperative Programming Relies on modifying on a state by using a sequence of commands The state is modified by assignment command. E.g., v = E or v := E. Commands can be: Sequenced, e.g., C1 ; C2 Conditionally executed, e.g., if Repeated, e.g., while Programs are a series of instructions on how to modify the state. Imperative (or procedural) languages, e.g., Fortran, Pascal, and C, support this style of programming.

An Abstract View A program execution can be modeled as a sequence of state changes starting from an initial state. s0  s1  s2  …  sn

Functional Programming A functional program is simply an expression, and executing the program means evaluating the expression. No state, i.e., variables No assignment No sequencing and repetition On the positive side, one can have: Recursive functions Flexibility, e.g., higher-order functions Functional languages, e.g., Lisp, Scheme, SML, Haskell, support this style of programming.

Example --- Factorial In Mathematics, factorial function, f : N  N, is defined as: 1 if x = 0 x * f(x-1) otherwise f(x) = In C, factorial function written imperatively as: int fact(int n) { int x = 1; while (n > 0) { x = x * n; n = n - 1; } return x;

Example --- Factorial (Cont.) In Mathematics, factorial function, f : N  N, is defined as: 1 if x = 0 x * f(x-1) otherwise f(x) = In Haskell fact 0 = 1 fact n = n * fact (n – 1) fact n | n == 0 = 1 | n > 0 = n * fact (n – 1) fact n | n == 0 = 1 | otherwise = n * fact (n – 1)

Merits of Functional Programming By avoiding variables and assignments: Clear semantics (tied to mathematical objects) More freedom in implementation, e.g., parallelization By the more flexible use of functions: Conciseness and elegance Better parameterization and modularity of programs Convenient ways of representing infinite data

Problems with Functional Programming Some things are harder to fit into a purely functional model, e.g., Input-output Interactive or continuously running programs (e.g., editors, process controllers) Functional languages also correspond less closely to current hardware, and thus they can be less efficient and it can be hard to reason about time and space usage.

In Sum, 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

Outline Imperative vs. functional programming Functions and lambda calculus

Mathematical Functions A mathematical function is a mapping of members of one set, called the domain set, to another set, called the range set. f: N+  N+ 1 1   10 100   domain Range

Higher-Order Functions A higher-order function, or functional form, is one that either takes functions as parameters or yields a function as its result, or both. Examples Function composition Construction Apply-to-all

Functional 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 (or g ; f) which means h (x)  f (g ( x)) For f (x)  x * x * x and g (x)  x + 3, h  f ° g yields (x + 3)* (x + 3)* (x + 3)

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, g|] For f (x)  x * x * x and g (x)  x + 3, [|f, g|] (4) yields [64, 7]

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]

Lambda Calculus Lambda notation is a way of denoting functions Example Invented by Alonzo Church in the 1930s. x.E[x], ‘the function of x that yields E[x]’ Example x.x Squaring function?

Why Lambda? In informal mathematics, when talking about a function, one normally gives it a name, e.g., “define f(x) = E[x] … then … f …” Similarly, most programming languages will only let you define functions if you are prepared to give them a name. This approach is rather inconsistent, as we are not treating functions on a par with other mathematical objects. E.g., we don’t say “define x and y by x = 2 and y = 4 respectively. Then x*x = y.” Lambda notation helps to put functions on an equal footing. This is important in functional programming.

Example --- Currying Functions of more than one argument x.y.x + y Q: Signature? Q: (x.y.x + y) 1 2 Covention x y.E[x,y] means x.y.E[x,y]

Exercise (5 Minutes In Pairs) Evaluate the following  expressions. (x.x) (x.x) (x.y.(y x)) 0 (x.x) (x.y.(x y)) ((x.x) (x.x)) (x.x) 21

Formal Lamda Calculus Syntax of lambda expressions <lambda-expr> -> <variable> | <constant> | <application> | <abstraction> <application> -> ( <lambda-expr> ) <lambda-expr> <abstraction> ->  <variable> . <lambda-expr> Examples x, y, … 0, 1, false, true, … (x.x) 0, (x.x) false, … x.x, …

Exercise Extend the lambda expression syntax to include unary operators, binary operators, and if-then-else expression. <lambda-expr> -> … | <unary-opr> <lambda-expr> | <lambda-expr> <binary-opr> <lambda-expr> | if <lambda-expr> then <lambda-expr> else <lambda-expr> Examples -x, x + y, if x > 0 then 1 else -1, …

Exercise Define a lambda abstraction of the following function, f : R  R, defined as f(x) = 0 if x = 0 x2sin(1/x2) if x != 0 \x.if x == 0 then 0 else x * x * sin (1 / (x*x))

Local Definitions <lambda-expr> -> … | let <name> = <lambda-expr> in <lambda-expr> Examples let x = 10 in x let incr = x.x+1 in incr 10 let f = x.if x == 0 then 1 else x * f (x – 1) in f 3

Group Exercise In lambda notation, define higher-order functions of functional composition, construction, and apply-to-all. For construction and apply-to-all, you may use Haskell lists operators and functions, such as: [] – empty list : – concatenation of element and list, e.g., 1 : [2]  [1, 2] length – number of elements in a list, e.g., length [2, 3]  2 head – first element of a list, e.g., head [1, 2]  1 tail – list containing all elements except for the first, e.g., tail [1, 2, 3]  [2, 3], tail [1]  [] \f.\g.\x.f (g x) let c = \l.\x.if (length l) == 0 then [] else (head l x) : c (tail l) x in c let a = \f.\l.if (length l) == 0 then [] else (f (head l)) : a f (tail l) in a

In Sum, Fundamentals of FPL 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 an FPL, variables are not necessary, as is the case in mathematics.

Fundamentals of FPL (Cont.) In an FPL, the evaluation of a function always produces the same result given the same parameters. This is called referential transparency.