COP4020 Programming Languages Functional Programming Prof. Xin Yuan.

Slides:



Advertisements
Similar presentations
Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
Advertisements

1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 14 Functional Programming Languages - The design of the imperative languages is based directly.
Lists in Lisp and Scheme a. Lists are Lisp’s fundamental data structures, but there are others – Arrays, characters, strings, etc. – Common Lisp has moved.
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.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
1 Programming Languages and Paradigms Lisp Programming.
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.
PZ10CX Programming Language design and Implementation -4th Edition Copyright©Prentice Hall, PZ10CX - LISP Programming Language Design and Implementation.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
6.001 SICP SICP Sections 5 & 6 – Oct 5, 2001 Quote & symbols Equality Quiz.
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.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
COMP 205 – Week 11 Dr. Chunbo Chu. Intro Lisp stands for “LISt Process” Invented by John McCarthy (1958) Simple data structure (atoms and lists) Heavy.
(5.1) COEN Functional Languages  Functional programming basics  Atoms and lists; cons  Useful primitive functions  Predicates  Arithmetic functions.
Comp. Eng. SW Lab II: FP with Scheme 1 Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office.
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
Functional Programming in Scheme
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
ISBN Chapter 15 Functional Programming Languages.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
Introduction to Scheme CS 480/680 – Comparative Languages “And now for something completely different…” – Monty Python “And now for something completely.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
CS 330 Programming Languages 11 / 13 / 2008 Instructor: Michael Eckmann.
Functional Programming in Scheme and Lisp.
COP4020 Programming Languages Functional Programming Prof. Robert van Engelen.
18-October-2002cse Symbols © 2002 University of Washington1 Symbols CSE 413, Autumn 2002 Programming Languages
ISBN Chapter 15 Functional Programming Languages.
Functional Programming CS331 Chapter 14. Functional Programming Original functional language is LISP –LISt Processing –The list is the fundamental data.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
CS220 Programming Principles 프로그래밍의 이해 2002 가을학기 Class 6 한 태숙.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
LECTURE 16 Functional Programming. WHAT IS FUNCTIONAL PROGRAMMING? Functional programming defines the outputs of a program as a mathematical function.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office Functional Programming.
Comparative Programming Languages Functional programming with Lisp/Scheme.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (n-n-n-notes) Summer 2003 Dr. Carter Tiernan.
1 Introduction to Functional Programming in Racket CS 270 Math Foundations of CS Jeremy Johnson.
COP4020 Programming Languages Functional Programming Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
Functional Programming
Functional Programming
Additional Scheme examples
Functional Programming Languages
Functional Programming
History of Computing – Lisp
Introduction to Scheme
Chapter 15 – Functional Programming Languages
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Lists in Lisp and Scheme
COP4020 Programming Languages
FP Foundations, Scheme In Text: Chapter 14.
Lecture #9 מבוא מורחב.
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
6.001 SICP Variations on a Scheme
Functional Programming: Lisp
6.001 SICP Interpretation Parts of an interpreter
COP4020 Programming Languages
Modern Programming Languages Lecture 18 Fakhar Lodhi
Lisp.
Lecture 25: The Metacircular Evaluator Eval Apply
Presentation transcript:

COP4020 Programming Languages Functional Programming Prof. Xin Yuan

COP4020 Spring /4/2016 Topics Functional programming with Scheme  Language constructs

COP4020 Spring /4/2016 Data Structures in scheme An expression operates on values and compound data structures built from atoms and lists A value is either an atom or a compound list Atoms are  Numbers, e.g. 7 and 3.14  Characters: #\a  Strings, e.g. "abc"  Boolean values #t (true) and #f (false)  Symbols, which are identifiers escaped with a single quote, e.g. 'y  The empty list () When entering a list as a literal value, escape it with a single quote  Without the quote it is a function invocation!  For example, '(a b c) is a list while (a b c) is a function application  Lists can be nested and may contain any value, e.g. '(1 (a b) ''s'')

COP4020 Spring /4/2016 Checking the Type of a Value The type of a value can be checked with  (boolean? x); is x a Boolean?  (char? x); is x a character?  (string? x); is x a string?  (symbol? x); is x a symbol?  (number? x); is x a number?  (list? x); is x a list?  (pair? x); is x a non-empty list?  (null? x); is x an empty list? Examples  (list? '(2))  #t  (number? ''abc'')  #f Portability note: on some systems false (#f) is replaced with ()

COP4020 Spring /4/2016 Working with Lists (car xs) returns the head (first element) of list xs (cdr xs) (pronounced "coulder") returns the tail of list xs (cons x xs) joins an element x and a list xs to construct a new list (list x 1 x 2 … x n ) generates a list from its arguments Examples:  (car '(2 3 4))  (car '(2))  (car '())  (cdr '(2 3))  (car (cdr '(2 3 4)))  (cdr (cdr '(2 3 4)))  (cdr '(2))  (cons 2 '(3))  (cons 2 '(3 4))  (list 1 2 3)

COP4020 Spring /4/2016 Working with Lists (car xs) returns the head (first element) of list xs (cdr xs) (pronounced "coulder") returns the tail of list xs (cons x xs) joins an element x and a list xs to construct a new list (list x 1 x 2 … x n ) generates a list from its arguments Examples:  (car '(2 3 4))  2  (car '(2))  2  (car '())  Error  (cdr '(2 3))  (3)  (car (cdr '(2 3 4)))  3; also abbreviated as (cadr '(2 3 4))  (cdr (cdr '(2 3 4)))  (4); also abbreviated as (cddr '(2 3 4))  (cdr '(2))  ()  (cons 2 '(3))  (2 3)  (cons 2 '(3 4))  (2 3 4)  (list 1 2 3)  (1 2 3)

How to represent some common data structure? Everything is a list:  Array? int A[4]= {1, 2, 3, 4}  2-d array: int a[4][4] = {{0, 0,0,0}, {1, 1,1,1}, {2,2,2,2}, {3,3,3,3}}  Structure (array)? struct ex {int a; char b;}  An array of a structure? struct ex {int a; char b;} aa[3] = {{10, ‘a’}, {20, ‘b’}, {30, ‘c’}};  A tree? Can use pre-order traversal list form (root left_tree right_tree) The major difference between scheme data structures and C++ data structures? -- no random access mechanism, list items are mostly accessed through car and cdr functions. COP4020 Spring /4/2016

COP4020 Spring /4/2016 The “if” Special Form Special forms resemble functions but have special evaluation rules  Evaluation of arguments depends on the special construct The “if” special form returns the value of thenexpr or elseexpr depending on a condition (if condition thenexpr elseexpr) Examples  (if #t 1 2)  (if #f 1 "a")  (if (string? "s") (+ 1 2) 4)  (if (> 1 2) "yes" "no")

COP4020 Spring /4/2016 The “if” Special Form Examples  (if #t 1 2)  1  (if #f 1 "a")  "a"  (if (string? "s") (+ 1 2) 4)  3  (if (> 1 2) "yes" "no")  "no"

COP4020 Spring /4/2016 The “cond” Special Form A more general if-then-else can be written using the “cond” special form that takes a sequence of (condition value) pairs and returns the first value x i for which condition c i is true: (cond (c 1 x 1 ) (c 2 x 2 ) … (else x n ) ) Note: “else” is used to return a default value Examples  (cond (#f 1) (#t 2) (#t 3) )  (cond (( = 1 2) ''two'') )  (cond ((< 2 1) 1) ((= 2 1) 2) (else 3) )  (cond (#f 1) (#f 2))

COP4020 Spring /4/2016 The “cond” Special Form Examples  (cond (#f 1) (#t 2) (#t 3) )  2  (cond (( = 1 2) ''two'') )  ''one''  (cond ((< 2 1) 1) ((= 2 1) 2) (else 3) )  3  (cond (#f 1) (#f 2))  error (unspecified value)

COP4020 Spring /4/2016 Logical Expressions Relations  Numeric comparison operators, >= Boolean operators  (and x 1 x 2 … x n ), (or x 1 x 2 … x n ) Other test operators  (zero? x), (odd? x), (even? x)  (eq? x 1 x 2 ) tests whether x 1 and x 2 refer to the same object (eq? 'a 'a)  #t (eq? '(a b) '(a b))  #f  (equal? x 1 x 2 ) tests whether x 1 and x 2 are structurally equivalent (equal? 'a 'a)  #t (equal? '(a b) '(a b))  #t  (member x xs) returns the sublist of xs that starts with x, or returns () (member 5 '(a b))  () (member 5 '( ))  (5 6)

COP4020 Spring /4/2016 Lambda Calculus: Functions = Lambda Abstractions A lambda abstraction is a nameless function (a mapping) specified with the lambda special form: (lambda args body) where args is a list of formal arguments and body is an expression that returns the result of the function evaluation when applied to actual arguments A lambda expression is an unevaluated function Examples:  (lambda (x) (+ x 1))  (lambda (x) (* x x))  (lambda (a b) (sqrt (+ (* a a) (* b b))))

COP4020 Spring /4/2016 Lambda Calculus: Invocation = Beta Reduction A lambda abstraction is applied to actual arguments using the familiar list notation (function arg 1 arg 2... arg n ) where function is the name of a function or a lambda abstraction Beta reduction is the process of replacing formal arguments in the lambda abstraction’s body with actuals  Defined in terms of substitution: ((λV.E) E′) is E[V := E′] Examples  ( (lambda (x) (* x x)) 3 )  (* 3 3)  9  ( (lambda (f a) (f (f a))) (lambda (x) (* x x)) 3 )  (f (f 3))where f = (lambda (x) (* x x))  (f ( (lambda (x) (* x x)) 3 ))where f = (lambda (x) (* x x))  (f 9)where f = (lambda (x) (* x x))  ( (lambda (x) (* x x)) 9 )  (* 9 9)  81

COP4020 Spring /4/2016 More Lambda abstractions Define a lambda abstraction that takes three parameters and returns the maximum value of the three. Define a lambda abstraction that takes two parameters, one scheme arithmetic expression (e.g. ( )), and the other one a number. The function return true (#t) if the expression evaluate to the number, false (#f) otherwise.

COP4020 Spring /4/2016 More Lambda abstractions Define a lambda abstraction that takes three parameters and returns the maximum value of the three.  (lambda (x y z) (if (> x y) (if (> x z) x z) (if (> y z) y z))) Define a lambda abstraction that takes two parameters, one scheme arithmetic expression (e.g. ( )), and the other one a number. The function return true (#t) if the expression evaluate to the number, false (#f) otherwise.  (lambda (x y) (if (equal? x y) #t #f) Functions are used much more liberally in scheme (compared to C++); the boundary before data and program is not as clear as in C++.