1 COMP313A Functional Programming (1). 2 Main Differences with Imperative Languages Say more about what is computed as opposed to how Pure functional.

Slides:



Advertisements
Similar presentations
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
Advertisements

1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
Programming Languages and Paradigms
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.
CSE 3302 Programming Languages Chengkai Li Fall 2007 Functional Programming Language (Introduction and Scheme) Lecture 17 – Functional Programming, Spring.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
4. Functional Programming. © O. Nierstrasz PS — Functional Programming 4.2 Roadmap Overview  Functional vs. Imperative Programming  Pattern Matching.
0 PROGRAMMING IN HASKELL Chapter 1 - Introduction.
Chapter 15 Other Functional Languages. Copyright © 2007 Addison-Wesley. All rights reserved. Functional Languages Scheme and LISP have a simple syntax.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 14 Functional Programming It is better to.
ISBN Chapter 15 Functional Programming Languages.
1 COMP313A Functional Programming (1). 2 Main Differences with Imperative Languages Say more about what is computed as opposed to how Pure functional.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Introduction to Functional Programming Notes for CSCE 190 Based on Sebesta,
ISBN Chapter 15 Functional Programming Languages.
0 PROGRAMMING IN HASKELL An Introduction Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
Functional Languages. Why? Referential Transparency Functions as first class objects Higher level of abstraction Potential for parallel execution.
The Evolution of Programming Languages Day 2 Lecturer: Xiao Jia The Evolution of PLs1.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
ISBN Chapter 15 Functional Programming Languages.
1 Programming Languages and Paradigms Functional Programming.
1 Functional Programming & Standard ML Hossein Hojjat et al.
Functional Programming With examples in F#. Pure Functional Programming Functional programming involves evaluating expressions rather than executing commands.
Chapter Fifteen: Functional Programming Languages Lesson 12.
COP4020 Programming Languages Functional Programming Prof. Robert van Engelen.
Compiling Functional Programs Mooly Sagiv Chapter 7
CS 363 Comparative Programming Languages Functional Languages: Scheme.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
1 Chapter 15 © 2002 by Addison Wesley Longman, Inc Introduction - The design of the imperative languages is based directly on the von Neumann architecture.
TIVDM2Functional Programming Language Concepts 1 Concepts from Functional Programming Languages Peter Gorm Larsen.
COMP313A Functional Programming (1)
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell 101 Dr. Hyunyoung Lee.
Logical and Functional Programming
Dr. Philip Cannata 1 Programming Languages Chapter 14 – Functional Programming – Lisp.
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 
Haskell Introduction CSCE 314 Spring CSCE 314 – Programming Studio Historical Background 1930s: Alonzo Church develops the lambda calculus, a simple.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
Erik Meijer FP101x - Functional Programming Programming in Haskell - Introduction.
Lambda Calculus A fairly simple system for representing functions The base of typed functional programming languages such as ML and Haskell.
C H A P T E R E I G H T Functional Programming Programming Languages – Principles and Paradigms by Allen Tucker, Robert Noonan.
1 PROGRAMMING IN HASKELL An Introduction Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources)
Functional Programming
Functional Programming
Functional Programming Languages
Zuse’s Plankalkül – 1945 Never implemented Problems Zuse Solved
What is a Functional Language?
CS 550 Programming Languages Jeremy Johnson
Functional Programming
CS 3304 Comparative Languages
A lightening tour in 45 minutes
Closures and Streams cs784(Prasad) L11Clos
FP Foundations, Scheme In Text: Chapter 14.
6.001 SICP Variations on a Scheme
CSE 3302 Programming Languages
PROGRAMMING IN HASKELL
L Calculus.
Closures and Streams cs7100(Prasad) L11Clos
COP4020 Programming Languages
Functional Programming and Haskell
15.2 Mathematical Functions
PROGRAMMING IN HASKELL
Chapter 15 Functional Programming 6/1/2019.
More Scheme CS 331.
Functional Programming and Haskell
Presentation transcript:

1 COMP313A Functional Programming (1)

2 Main Differences with Imperative Languages Say more about what is computed as opposed to how Pure functional languages have no –variables –assignments –other side effects Means no loops

3 Differences… Imperative languages have an implicit state (state of variables) that is modified (side effect). Sequencing is important to gain precise, deterministic control over the state assignment statement needed to change the binding for a particular variable (alter the implicit state)

4 Imperative factorial n:=x; a:=1; while n > 0 do begin a:= a * n; n := n-1; end;

5 Differences Declarative languages have no implicit state Program with expressions The state is carried around explicitly –e.g. the formal parameter n in factorial Looping is accomplished with recursion rather than sequencing

6 Haskell fac ::Int -> Int fac n | n == 0 = 1 | n > 0 = fac(n-1) * n | otherwise = 0 Scheme (define fac (lambda (n) (if (= n 0) 1 (* n (fac (- n 1))))))

7 Advantages similar to traditional mathematics referential transparency makes programs more readable higher order functions give great flexibility concise programs

8 Referential Transparency the value of a function depends only on the values of its parameters for example Haskell …x + x… where x = f a

9 Evolution of Functional languages Lambda Calculus Alonzo Church Captures the essence of functional programming Formalism for expressing computation by functions Lambda abstraction In Scheme In Haskell ( x. + 1 x) (lambda(x) (+ 1 x))) add1 :: Int -> Int add1 x = 1 + x

10 Lambda Calculus… application of expressions reduction rule that substitutes 2 for x in the lambda (beta reduction) ( x. + 1 x) 2 ( x. + 1 x) 2  (+ 1 2)  3 ( x. * x x) (+ 2 3) i.e. (+ 2 3) / x)

11 Lambda Calculus… ( x.E) –variable x is bound by the lambda –the scope of the binding is the expression E ( x. + y x) –( x. + y x)2  (+ y 2) ( x. + (( y. (( x. * x y) 2)) x) y) 

12 Lambda Calculus… Each lambda abstraction binds only one variable Need to bind each variable with its own lambda. ( x. ( y. + x y))

13 Lisp McCarthy late 1950s Used Lambda Calculus for anonymous functions List processing language First attempt built on top of FORTRAN

14 LISP… McCarthy’s main contributions were 1.the conditional expression and its use in writing recursive functions Scheme (define fac (lambda (n) (if (= n 0) 1 (if (> n 0) (* n (fac (- n 1))) 0)))) Scheme (define fac2 (lambda (n) (cond ((= n 0) 1) ((> n 0) (* n (fac2 (- n 1)))) (else 0)))) Haskell fac ::Int -> Int fac n | n == 0 = 1 | n > 0 = fac(n-1) * n | otherwise = 0 Haskell fac :: Int -> Int fac n = if n == 0 then 1 else if n > 0 then fac(n-1) * n else 0

15 2.the use of lists and higher order operations over lists such as mapcar Lisp… Scheme (mymap + ’(3 4 6) ’(5 7 8)) Haskell mymap :: (Int -> Int-> Int) -> [Int] -> [Int] ->[Int] mymap f [] [] = [] mymap f (x:xs) (y:ys) = f x y : mymap f xs ys add :: Int -> Int -> Int add x y = x + y Scheme (define mymap (lambda (f a b) (cond ((and (null? a) (null? b)) '()) (else (cons (f (first a) (first b)) (mymap f (rest a) (rest b))))))) Haskell mymap add [3, 4, 6] [5, 7, 8]

16 Lisp 3.cons cell and garbage collection of unused cons cells Scheme (cons ’1 ’( )) ( ) Haskell cons :: Int -> [Int] -> [Int] cons x xs = x:xs Scheme (define mymap (lambda (f a b) (cond ((and (null? a) (null? b)) '()) (else (cons (f (first a) (first b)) (mymap f (first a) (first b)))))))

17 Lisp… 4.use of S-expressions to represent both program and data An expression is an atom or a list But a list can hold anything…

18 Scheme (cons ’1 ’( )) ( ) Scheme (define mymap (lambda (f a b) (cond ((and (null? a) (null? b)) '()) (else (cons (f (first a) (first b)) (mymap f (first a) (first b)))))))

19 ISWIM Peter Landin – mid 1960s If You See What I Mean Landon wrote “…can be looked upon as an attempt to deliver Lisp from its eponymous commitment to lists, its reputation for hand-to-mouth storage allocation, the hardware dependent flavour of its pedagogy, its heavy bracketing, and its compromises with tradition”

20 Iswim… Contributions –Infix syntax –let and where clauses, including a notion of simultaneous and mutually recursive definitions –the off side rule based on indentation layout used to specify beginning and end of definitions –Emphasis on generality small but expressive core language

21 let in Scheme let* - the bindings are performed sequentially (let * ((x 2) (y 3))  ( * x y)) (let * ((x 2) (y 3)) (let * ((x 7) (z (+ x y)))  ( * z x)))

22 let in Scheme (let ((x 2) (y 3))  ? ( * x y)) let - the bindings are performed in parallel, i.e. the initial values are computed before any of the variables are bound (let ((x 2) (y 3)) (let ((x 7) (z (+ x y)))  ? ( * z x)))

23 letrec in Scheme letrec – all the bindings are in effect while their initial values are being computed, allows mutually recursive definitions (letrec ((even? (lambda (n) (if (zero? n) #t (odd? (- n 1))))) (odd? (lambda (n) (if (zero? n) #f (even? (- n 1)))))) (even? 88))

24 ML Gordon et al 1979 Served as the command language for a proof generating system called LCF –LCF reasoned about recursive functions Comprised a deductive calculus and an interactive programming language – Meta Language (ML) Has notion of references much like locations in memory I/O – side effects But encourages functional style of programming

25 ML Type System –it is strongly and statically typed –uses type inference to determine the type of every expression –allows polymorphic functions Has user defined ADTs

26 SASL, KRC, and Miranda Used guards Higher Order Functions Lazy Evaluation Currying SASL fac n = 1,n = 0 = n * fac (n-1), n>0 Haskell fac n | n ==0 = 1 | n >0 = n * ( fac(n-1) add x y = + x y add 1 switch :: Int -> a -> a -> a switch n x y | n > 0 = x | otherwise = y multiplyC :: Int -> Int -> Int Versus multiplyUC :: (Int, Int) -> Int

27 SASL, KRC and Miranda KRC introduced list comprehension Miranda borrowed strong data typing and user defined ADTs from ML comprehension :: [Int] -> [Int] comprehension ex = [2 * n | n <- ex]