Announcements HW4 due today. There is no HW5. (It was merged into HW4.) HW6 (Scheme, Problem 1) out today, due October 23rd We’ll cover 100% of what you.

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 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
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.
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.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
Functional Design and Programming Lecture 1: Functional modeling, design and programming.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
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.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Functional Programing Referencing material from Programming Language Pragmatics – Third Edition – by Michael L. Scott Andy Balaam (Youtube.com/user/ajbalaam)
Functional Programming in Scheme
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 Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig.
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.
Chapter Fifteen: Functional Programming Languages Lesson 12.
ISBN Chapter 15 Functional 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.
Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
Scheme Profs Tim Sheard and Andrew Black CS 311 Computational Structures.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
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 
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.
Spring 16 CSCI 4430, A Milanova/BG Ryder 1 Announcements HW5 due March 28 Homework Server link is up I will have office hours, Fri or Mon, check Announcements.
1 Introduction to Functional Programming in Racket CS 270 Math Foundations of CS Jeremy Johnson.
Functional Programming
CS314 – Section 5 Recitation 9
Operational Semantics of Scheme
Functional Programming
Functional Programming Languages
Functional Programming
History of Computing – Lisp
CS 3304 Comparative Languages
ML: a quasi-functional language with strong typing
CS 326 Programming Languages, Concepts and Implementation
CS 326 Programming Languages, Concepts and Implementation
Introduction to Scheme
CS 326 Programming Languages, Concepts and Implementation
CS 326 Programming Languages, Concepts and Implementation
Scheme : variant of LISP
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Introduction to Functional Programming in Racket
Mini Language Interpreter Programming Languages (CS 550)
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming Languages
CS 36 – Chapter 11 Functional programming Features Practice
Announcements Quiz 6 HW7 due Tuesday, October 30
6.001 SICP Variations on a Scheme
Introduction to Functional Programming in Racket
Announcements Exam 2 on Friday, November 2nd Topics
Announcements Quiz 5 HW6 due October 23
15.2 Mathematical Functions
topics interpreters meta-linguistic abstraction eval and apply
More Scheme CS 331.
Presentation transcript:

Announcements HW4 due today. There is no HW5. (It was merged into HW4.) HW6 (Scheme, Problem 1) out today, due October 23rd We’ll cover 100% of what you need today! Rainbow grades available HW1-3, Quiz1-3, Exam1

Last Class Static semantic analysis Attribute grammars Synthesized and inherited attributes S-attributed grammars L-attributed grammars Fall 18 CSCI 4430, A Milanova/BG Ryder

Today’s Lecture Outline Functional programming languages Scheme S-expressions and lists cons, car, cdr Defining functions Examples of recursive functions Shallow recursion, Deep recursion Equality testing Higher order functions Binding with let and let* Fall 18 CSCI 4430, A Milanova/BG Ryder

Functional Programming with Scheme Read: Scott, Chapter 11

Racket/PLT Scheme/DrScheme Download Racket (was PLT Scheme (was DrScheme)) http://racket-lang.org/ Run DrRacket, choose language R5RS Online tutorial: Teach yourself Scheme in Fixnum days: http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme.html Fall 18 CSCI 4430, A Milanova

First, Imperative Languages The concept of assignment is central X:=5; Y:=10; Z:=X+Y; W:=f(Z); Side effects on memory Program semantics (i.e., how the program works): state-transition semantics A program is a sequence of assignment statements with effect on memory (i.e., state) Let us look at imperative languages. What are the defining characteristics of imperative languages? 1. C := 0; 2. for I := 1 step 1 until N do 3. C := C + a[I]* b[I]; Fall 18 CSCI 4430, A Milanova

Imperative Languages Functions (typically called “procedures” or “subroutines”) have side effects: Roughly: A function call affects visible state; i.e., a function call affects execution of other functions, and in general, function calls cannot be replaced by result Also, result of a function call depends on visible state; i.e., function call is not independent of the context of the call In imperative languages we use terms procedure, or subroutine, not function. For obvious reasons . Fall 18 CSCI 4430, A Milanova/BG Ryder

Imperative Languages Functions are, traditionally, not first-class values A first-class value is one that can be passed as argument to functions, and returned as result from functions In a language with assignments, it can be assigned into a variable or structure Are functions in C first-class values? As languages become more multi-paradigm, imperative languages increasingly support functions as first-class values (JS, R, Python, Scala, Java 8, C++11) Fall 18 CSCI 4430, A Milanova/BG Ryder

Functional Languages Program semantics: reduction semantics A program is a set of function definitions and their application to arguments Def IP = (Insert +) º (ApplyToAll *) º Transpose IP <<1,2,3>,<6,5,4>> is (Insert +) ((ApplyToAll *) (Transpose <<1,2,3>,<6,5,4>>)) (Insert +) ((ApplyToAll *) <<1,6>,<2,5>,<3,4>>) (Insert +) <6,10,12> 28 Function composition Fall 18 CSCI 4430, A Milanova/BG Ryder

Functional Languages In pure functional languages, there is no notion of assignment, no notion of state Variables are bound to values only through parameter associations No side effects! Referential transparency Roughly: Result of function application is independent of context where the function application occurs; function application can be replaced by result Fall 18 CSCI 4430, A Milanova/BG Ryder

Functional Languages Functions are first-class values Can be returned as value of a function application Can be passed as an argument In a language with assignment, can be assigned into variables and structures Unnamed functions exist as values Fall 18 CSCI 4430, A Milanova/BG Ryder

Lisp and Scheme Lisp is the second-oldest high-level programming language! Simple syntax! Program code and data have same syntactic form The S-expression Function application written in prefix form (e1 e2 e3 … ek) means Evaluate e1 to a function value Evaluate each of e2,…,ek to values Apply the function to these values (+ 1 3) evaluates to 4 Fall 18 CSCI 4430, A Milanova/BG Ryder

History Lisp Scheme Common Lisp 1950’s 1975 John McCarthy Guy Steele Gerald Sussman Common Lisp dynamic scoping lexical scoping functions as first class values Fall 18 CSCI 4430, A Milanova/BG Ryder

Why Scheme? Simple! Great to introduce core functional programming concepts Reduction semantics Lists and recursion Higher order functions Evaluation order Parametric polymorphism Later we’ll tackle Haskell and new concepts Algebraic data types and pattern matching Lazy evaluation Type inference

S-expressions S-expr ::= Name | Number | ( { S-expr } ) Name is a symbolic constant (a string of chars which starts off with anything that can’t start a Number) Number is an integer or real number List of zero or more S-expr’s E.g., (a (b c) (d)) is a list S-expr a Syntax is given in Extended Backus Naur Form (EBNF). An S-expr is a Name (a symbolic constant) or a Number (a numerical constant), or a list of zero or more S-expr’s. Note the underlined ( and ). Parens are tokens in the language, and are underlined to differentiate with ( ) used as metasymbols in EBNF. { S-expr } denotes “one or more occurrences of S-expr” in EBNF. b c ( ) ( ) ( ) Fall 18 CSCI 4430, A Milanova/BG Ryder d

List Functions car and cdr cons () is the empty list Given a list, they decompose it into first element, rest-of-list portions E.g., car of (a (b c) (d)) is a E.g., cdr of (a (b c) (d)) is ((b c)(d)) cons Given an element and a list, cons builds a new list with the element as its car and the list as its cdr cons of a and (b) is (a b) () is the empty list IBM 704 had support for splitting a word. CAR stands for Content of the Address Register CDR stands for Content of the Data Register Fall 18 CSCI 4430, A Milanova/BG Ryder

Quoting ‘ or quote prevents the Scheme interpreter from evaluating the argument (quote (+ 3 4)) yields (+ 3 4) ‘(+ 3 4) yields (+ 3 4) Whereas (+ 3 4) yields 7 Why do we need quote? Fall 18 CSCI 4430, A Milanova/BG Ryder

Questions ((a) b (c d)) a () (car ‘(a b c)) yields ? (car ‘((a) b (c d))) yields ? (cdr ‘(a b c)) yields ? (cdr ‘((a) b (c d))) yields ? Can compose these operators in a short-hand manner. Can reach arbitrary list element by composition of car’s and cdr’s. (car (cdr (cdr ‘((a) b (c d)) ))) can also be written (caddr ‘((a) b (c d)) ) (car (cdr (cdr ‘((a) b (c d)) ))) = (car (cdr ‘( b (c d))) = (car ‘((c d)) = (c d) b c d () () Does this look familiar? (car ‘(a b c)) yields a (car ‘((a) b (c d))) yields (a) (cdr ‘(a b c)) gives (b c) (cdr ‘((a) b (c d))) gives (b (c d))

Questions Recall cons E.g., (cons ‘a ‘(b c)) yields (a b c) (cons ‘d ‘(e)) yields ? (cons ‘(a b) ‘(c d)) yields ? (cons ‘(a b c) ‘((a) b (c d))) yields ? (cons ‘(a b c) ‘((a) b (c d))) yields ((a b c) (a) b (c d)) (cons ‘d ‘(e)) yields (d e) (cons ‘(a b) ‘(c d)) yields ((a b) c d) Fall 18 CSCI 4430, A Milanova/BG Ryder

Type Predicates Note the quote: it prevents evaluation of the argument (symbol? ‘sam) yields #t (symbol? 1) yields #f (number? ‘sam) yields #f (number? 1) yields #t (list? ‘(a b)) yields #t (list? ‘a) yields #f (null? ‘()) yields #t (null? ‘(a b)) yields #f (zero? 0) yields #t (zero? 1) yields #f Can compose these. (zero? (- 3 3)) yields #t Note that since this language is fully parenthesized, there are no precedence problems in the expressions!

Question What is the typing discipline in Scheme? Static or dynamic? Answer: Dynamic typing. Variables are bound to values of different types at runtime. All type checking done at runtime. Fall 18 CSCI 4430, A Milanova/BG Ryder

Lecture Outline Functional Programming Languages Scheme S-expressions and lists cons, car, cdr Defining functions Examples of recursive functions Shallow recursion, Deep recursion Equality testing Higher order functions Binding with let and let* Fall 18 CSCI 4430, A Milanova/BG Ryder

Scheme: Defining Funcitons Fcn-def ::= (define (Fcn-name {Param}) S-expr) Fcn-name should be a new name for a function. Param should be variable(s) that appear in the S-expr which is the function body. Fcn-def ::= (define Fcn-name Fcn-value) Fcn-value ::= (lambda ( {Param} ) S-expr) where Param variables are expected to appear in the S-expr; called a lambda expression. Fall 18 CSCI 4430, A Milanova/BG Ryder

Examples (define (zerocheck? x) (if (= x 0) #t #f) ) If-expr ::= ( if S-expr0 S-expr1 S-expr2 ) where S-expr0 must evaluate to a boolean value; if that value is true, then the If-expr returns the value of S-expr1, else the value of S-expr2. (zerocheck? 1) yields #f, (zerocheck? (* 1 0)) yields #t a b Fall 18 CSCI 4430, A Milanova/BG Ryder

Examples (define (atom? object) (not (pair? object)) ) Here pair? is a built-in type predicate. It yields #t if the argument is a non-trivial S-expr (i.e., something you can take the cdr of). It yields #f otherwise. not is the built-in logical operator. What does atom? do? a b Fall 18 CSCI 4430, A Milanova/BG Ryder

Examples (define square (lambda (n) (* n n))) Associates the Fcn-name square with the function value (lambda (n) (* n n))) Lambda calculus is a formal theory of functions Set of functions definable using lambda calculus (Church 1941) is same as set of functions computable as Turing Machines (Turing 1930’s) Fall 18 CSCI 4430, A Milanova/BG Ryder

Trace of Evaluation (define (atom? object) (not (pair? object)) ) (atom? `(a)) -obtain function value corresponding to atom? -evaluate `(a) obtaining (a) -evaluate (not (pair? ‘(a))) -obtain function value corresponding to not -evaluate (pair? ‘(a)) -obtain function value corresponding to pair? -evaluate ‘(a) obtaining (a) -return value #t -return #f Fall 18 CSCI 4430, A Milanova/BG Ryder

Read-eval-print Loop Scheme interpreter runs read-eval-print loop Read input from user A function application Evaluate input (e1 e2 e3 … ek) Evaluate e1 to obtain a function Evaluate e2, … , ek to values Execute function body using values from previous step as parameter values Return value Print return value Fall 18 CSCI 4430, A Milanova/BG Ryder

Fall 18 CSCI 4430, A Milanova/BG Ryder

Conditional Execution (if e1 e2 e3) (cond (e1 h1) (e2 h2) … (en-1 hn-1) (else hn)) Cond is like if – then – else if construct (define (zerocheck? x) (cond ((= x 0) #t) (else #f))) OR (define (zchk? x) (cond ((number? x) (zero? x)) (else #f))) Fall 18 CSCI 4430, A Milanova/BG Ryder

Recursive Functions (len `(1 2)) should yield 2. (define (len x) (cond ((null? x) 0) (else (+ 1 (len (cdr x)))))) (len `(1 2)) should yield 2. Trace: (len `(1 2)) -- top level call x = (1 2) (len `(2)) -- recursive call 1 x = (2) (len `()) -- recursive call 2 x = () returns 0 -- return for call 2 returns (+ 1 0) = 1 --return for call 1 returns (+ 1 1) = 2 -- return for top level call (len ‘((a) b (c d))) yields what? len is a shallow recursive function

Recursive Functions (define (app x y) (cond ((null? x) y) ((null? y) x) (else (cons (car x) (app (cdr x) y))))) What does app do? (app ‘() ‘()) yields ? (app ‘() ‘(1 4 5)) yields ? (app ‘(5 9) ‘(a (4) 6)) yields ? app is a shallow recursive function (app ‘() ‘()) yields () (app ‘() ‘(1 4 5)) yields (1 4 5) (app ‘(5 9) ‘(a (4) 6)) yields (5 9 a 9 (4) 6) Fall 18 CSCI 4430, A Milanova/BG Ryder

Exercise Write a version of len that uses if instead of cond (define (len x) (cond ((null? x) 0) (else (+ 1 (len (cdr x)))))) Write a version of len that uses if instead of cond Write a function countlists that counts the number of list elements in a list. E.g., (countlists ‘(a)) yields 0 (countlists ‘(a (b c (d)) (e))) yields 2 Recall (list? l) returns true if l is a list, false otherwise

Recursive Functions What does fun do? (define (fun x) (cond ((null? x) 0) ((atom? x) 1) (else (+ (fun (car x)) (fun (cdr x)))) )) fun is a deep recursive function What does fun do? Fall 18 CSCI 4430, A Milanova/BG Ryder

fun counts atoms in a list (define (atomcount x) (cond ((null? x) 0) ((atom? x) 1) (else (+ (atomcount (car x)) (atomcount (cdr x)))) )) (atomcount ‘(a)) yields 1 (atomcount ‘(1 (2 (3)) (5)) ) yields 4 Trace: (atomcount ‘(1 (2 (3)) ) 1> (+ (atomcount 1) (atomcount ‘( (2 (3)) ) )) 2> (+ (atomcount ‘(2 (3)) ) (atomcount ‘( ) ) ) 3> (+ (atomcount 2) (atomcount ‘((3)) ) 4> (+ (atomcount ‘(3)) (atomcount ‘( )) ) 5> (+ (atomcount 3) (atomcount ‘( ))) atomcount is a deep recursive function ??? 1 Spring 16 CSCI 4430, A Milanova/BG Ryder

Group Exercise Write a function flatten that flattens a list (flatten ‘(1 (2 (3)))) yields (1 2 3) Fall 18 CSCI 4430, A Milanova/BG Ryder

Lecture Outline Functional Programming Languages Scheme S-expressions and lists cons, car, cdr Defining functions Examples of recursive functions Shallow recursion, Deep recursion Equality testing Higher order functions Binding with let and let* Fall 18 CSCI 4430, A Milanova/BG Ryder

Equality Testing eq? eql? equal? Built-in predicate that can check atoms for equal values Doesn’t work on lists as you might expect! eql? Our predicate that works on lists (define (eql? x y) (or (and (atom? x) (atom? y) (eq? x y)) (and (not (atom? x)) (not (atom? y)) (eql? (car x) (car y)) (eql? (cdr x) (cdr y)) ))) equal? Built-in predicate that works on lists Fall 18 CSCI 4430, A Milanova/BG Ryder

Examples (eql? ‘(a) ‘(a)) yields what? (eql? ‘a ‘b) yields what? (eql? ‘b ‘b) yields what? (eql? ‘((a)) ‘(a)) yields what? (eq? ‘a ‘a) yields what? (eq? ‘(a) ‘(a)) yields what? Fall 18 CSCI 4430, A Milanova/BG Ryder

Models for Variables Value model for variables A variable is a location that holds a value I.e., a named container for a value a := b Reference model for variables A variable is a reference to a value Every variable is an l-value Requires dereference when r-value needed (usually, but not always implicit) l-value (the location) r-value (the value held in that location) Fall 18 CSCI 4430, A Milanova/BG Ryder

Models for Variables: Example c := b; a := b + c; Value model for variables b := 2 b: c := b c: a := b+c a: Reference model for variables b := 2 b c := b c a := b+c a 2 2 4 2 4 Fall 18 CSCI 4430, A Milanova/BG Ryder

Equality Testing: How does eq? work? Scheme uses the reference model for variables! (define (f x y) (list x y)) Call (f ‘a ‘a) yields (a a) x refers to atom a and y refers to atom a. eq? checks that x and y both point to the same place. Call (f ‘(a) ‘(a)) yields (‘(a) ‘(a)) x and y do not refer to the same list. x y a A cons cell. note that ( ) or nil is an atom! nil is actually a polymorphic constant as it stands both the empty list and is an atom as well. also, note that x and y don’t point to the same list at all so that the eq? comparison will fail. in the atom: a case, x and y both point to the cell which points to a, so they have the same contents and compare eq. safest thing to do is to use eql? on all comparisons! this tests that 2 S-exprs are isomorphic, which is what we usually mean by equal. x y a ( ) Fall 18 CSCI 4430, A Milanova/BG Ryder

Models for Variables C/C++, Pascal, Fortran Java JS, Python, R, etc. Value model Java Mixed model: value model for simple types, reference model for class types JS, Python, R, etc. Reference model Scheme Reference model! eq? is “reference equality” (akin of Java’s ==), equal? is value equality