CS 152: Programming Language Paradigms March 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.

Slides:



Advertisements
Similar presentations
1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
Advertisements

Computational Models The exam. Models of computation. –The Turing machine. –The Von Neumann machine. –The calculus. –The predicate calculus. Turing.
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.
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 Mathematical Functions Fundamentals of Functional Programming Languages Introduction to.
CSC321: Programming Languages14-1 Programming Languages Tucker and Noonan Chapter 14: Functional Programming 14.1 Functions and the Lambda Calculus 14.2.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
ISBN Chapter 15 Functional Programming Languages.
JavaScript, Third Edition
ISBN Chapter 15 Functional Programming Languages.
CS 152: Programming Language Paradigms April 9 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part I: Concepts and Scheme Programming Languages: Principles and Practice, 2nd Ed. Kenneth.
CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Functional Programming in Scheme
CSC3315 (Spring 2009)1 CSC 3315 Programming Paradigms Scheme Language Hamid Harroud School of Science and Engineering, Akhawayn University
CS 152: Programming Language Paradigms February 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
ISBN Chapter 15 Functional Programming Languages.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
ISBN Chapter 15 Functional Programming Languages.
CS 152: Programming Language Paradigms February 17 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS 152: Programming Language Paradigms March 10 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 8.
CS 152: Programming Language Paradigms April 2 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 152: Programming Language Paradigms February 26 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
ISBN Chapter 15 Functional Programming Languages.
CS 363 Comparative Programming Languages Functional Languages: Scheme.
Comparative Programming Languages Language Comparison: Scheme, Smalltalk, Python, Ruby, Perl, Prolog, ML, C++/STL, Java, Haskell.
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.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
Programming Languages Third Edition Chapter 4 Logic Programming.
CS 152: Programming Language Paradigms March 19 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS 152: Programming Language Paradigms March 5 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
For Friday No reading Prolog Handout 2. Homework.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
ISBN Chapter 15 Functional Programming Languages.
CS 152: Programming Language Paradigms April 7 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design October 12 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Copyright © 2009 Elsevier Chapter 6:: Control Flow Programming Language Pragmatics Michael L. Scott.
Ada, Scheme, R Emory Wingard. Ada History Department of Defense in search of high level language around Requirements drafted for the language.
A Short History of PL's (MacLennan 1999) “Zeroth Generation” (1940's-50's): machine language / assembly: if/then, read/write, store, goto
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
CS 152: Programming Language Paradigms May 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
Language Paradigms CS655.
Section 16.5, 16.6 plus other references
Functional Programming
Functional Programming Languages
Functional Programming Languages
Chapter 15 :Functional Programming Languages
Functional Programming Languages
CS 326 Programming Languages, Concepts and Implementation
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Computer Programming.
Chap. 6 :: Control Flow Michael L. Scott.
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming Languages
Chap. 6 :: Control Flow Michael L. Scott.
CS 36 – Chapter 11 Functional programming Features Practice
Functional Programming Languages
Programming Techniques
15.2 Mathematical Functions
Functional Programming Languages
Presentation transcript:

CS 152: Programming Language Paradigms March 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak

SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 2 Smalltalk IDE Class Categories Class Names Message Categories Message Selectors Text Section

SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 3 Screenshot of the original Smalltalk environment.

SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 4 Smalltalk Messages  Unary message: A message with no arguments.  Keyword message: A message that expect arguments. The message selector (method name) ends in a colon. Example:  includes is a boolean method that has as an element argument and returns whether or not the element is in the set.  Does the new set include the element 'Hello' ?  If a message has more than one argument, a keyword and colon must precede each argument. Example: at:put: is the complete message selector. Set new includes: 'Hello' a at: i+1 put: (a at: i).

SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 5 Smalltalk Messages, cont’d  Binary messages allow you to write arithmetic and comparison expressions with infix notation. Examples: "Returns 7" 3 < 4 "Returns true"

SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 6 Smalltalk Variables  Use variables to refer to objects. Example:  Temporary variables are declared between vertical bars They are not capitalized.  Smalltalk variables have no assigned data type A variable can name any thing. The assignment operator is := or 

SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 7 Smalltalk Variables, cont’d  Statements are separated by periods.  A sequence of messages to the same object are separated by semicolons: Example:  Smalltalk variables use reference semantics, not value semantics. A variable refers to an object. It does not contain an object.

SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 8 Smalltalk Equality and Identity Operators  The equality operator is =  The object identity operator is ==

SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 9 Smalltalk Statements  The to:do message creates a loop. Even control statements are expressed with message passing.  Enclose a code block with square brackets [ ] Similar to a Schema lambda form. A block can contain arguments as block variables. _

SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 10 Smalltalk Statements, cont’d  An ifTrue:ifFalse message expresses a choice of action.  Print the contents of an array: array do: [:element | Transcript nextPutAll: element printString; cr] "Print to the transcript each element followed by a carriage return."

SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 11 Review for the Midterm  A test of understanding, not memorization.  Open book, open notes, open laptop, open Internet. But not open neighbor! Forbidden to communicate with anyone else during the exam. _

SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 12 Review for Midterm, cont’d  Historical overview von Neumann architecture Machine and assembly languages FORTRAN COBOL Algol Pascal  Abstractions Data Control Basic Structured Unit

SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 13 Review for Midterm, cont’d  Paradigms Functional Logic Object-oriented  von Neumann architecture  von Neumann bottleneck  Language syntax  Language semantics  Compilers  Interpreters

SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 14 Review for Midterm, cont’d  Good language design Efficiency Regularity Generality Orthogonality Uniformity Security Extensibility _

SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 15 Review for Midterm, cont’d  Functional programming Programs as functions Recursion Referential transparency Lack of state First-class objects Higher-order functions  Scheme Atoms and lists Prefix notation Binding List construction and destruction

SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 16 Review for Midterm, cont’d  Scheme, cont’d Procedures and predicates Conditional expressions Recursion  flat  mutual  deep  tail recursion Symbol manipulation Symbolic differentiation Scope  local bindings  closure

SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 17 Review for Midterm, cont’d  Scheme, cont’d Procedure as a first-class object  as an argument  as a return value Metalinguistic power  eval  apply _

SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 18 Review for Midterm, cont’d  Logic Deductive reasoning  Prolog Declarative language Objects and relations Fact, rules, and questions Conjuctions Variables Backtracking  place markers Resolution Unification Cut

SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 19 Review for Midterm, cont’d  Object-oriented programming Software reuse and independence Extension Redefinition Abstraction Polymorphism Encapsulation Loose coupling Frameworks _

SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 20 Review for Midterm, cont’d  Smalltalk Purely object-oriented Objects Messages IDE _

SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 21 Sample Midterm Question: Scheme  Suppose you have a Scheme procedure min-to-head that takes an argument list of top-level integers and returns a list where the minimum element of the list is moved to the head. If there is a tie, it moves only one of the minimum elements. Example: (define nums '( )) (min-to-head nums)  ( )

SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 22 Sample Midterm Question: Scheme, cont’d  Write a recursive Scheme procedure sort that uses procedure min-to-head as a helper to sort an argument list of top-level integers and return a list where the elements are sorted in ascending order. Example: (sort nums)  ( )

SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 23 Sample Midterm Question: Scheme, cont’d Base case: The empty list. Procedure sort calls helper procedure min-to-head to move the smallest element to the head of the list. Call sort recursively on the remainder of the min-to-headed list in order to move the next smallest element to the second position. Continue until the base case. This is a selection sort. (define sort (lambda (lst) (if (null? lst) '() (let ((mth-lst (min-to-head lst))) (cons (car mth-lst) (sort (cdr mth-lst)))) )))

SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 24 Sample Midterm Question: Scheme, cont’d  Write a recursive Scheme procedure min-to-head. Base case: The empty list or a list containing only one element.  Otherwise, assume that min-to-head was successfully applied to the cdr of the list.  The procedure has moved the minimum value of the cdr of the list to the second position of the list.  Compare the car of the list to the second position.  If necessary, swap the two values to put the lesser value at the head of the list. (define min-to-head (lambda (lst) (cond ((null? lst) '()) ((null? (cdr lst)) lst) (else (let ((mth-cdr (min-to-head (cdr lst)))) (if (> (car lst) (car mth-cdr)) (append (list (car mth-cdr) (car lst)) (cdr mth-cdr)) lst))) )))

SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 25 Sample Midterm Question: Scheme, cont’d  Assume that min-to-head has correctly worked on the cdr of the list. Therefore, the second element of the list is the minimum within the cdr of the list.  Compare the car of the list (the green element) against the red element. If necessary, swap them. Now the minimum of the entire list is at the head.  Recursively call min-to-head on the cdr of the list.

SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 26 Sample Midterm Question: Prolog  Given this Prolog database and the query: Explain the result of the query in terms of resolution, unification, and backtracking. /*1*/ go(0, 0) :- !. /*2*/ go(Control, Control). /*3*/ go(Control, Start) :- Next is Start - 1, go(Control, Next). /*4*/ nasa(Start) :- go(Control, Start), write(Control), nl, fail. /*5*/ launch(Start) :- not(nasa(Start)), write('We have liftoff!'). ?- launch(10).

SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 27 Sample Midterm Question: Prolog, cont’d  Answer:  Resolution: The query launch(10) matches Rule 5.  Unification: Rule 5: Variable Start is instantiated and bound to 10.  Rule 5: First subgoal: nasa(10) matches Rule 4. _ /*1*/ go(0, 0) :- !. /*2*/ go(Control, Control). /*3*/ go(Control, Start) :- Next is Start - 1, go(Control, Next). /*4*/ nasa(Start) :- go(Control, Start), write(Control), nl, fail. /*5*/ launch(Start) :- not(nasa(Start)), write('We have liftoff!'). ?- launch(10).

SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 28 Sample Midterm Question: Prolog, cont’d  Rule 5: First subgoal: nasa(10) matches Rule 4.  Variable Start is bound to 10.  Rule 4: First subgoal: Match Fact 2.  Variable Control is instantiated and shared with Start which is 10.  Rule 4: First subgoal satisfied.  Rule 4: Second subgoal: write(Control)  print “10”.  Rule 4: Third subgoal: Print a new line.  Rule 4: Fourth subgoal: Fail. ?- launch(10). /*1*/ go(0, 0) :- !. /*2*/ go(Control, Control). /*3*/ go(Control, Start) :- Next is Start - 1, go(Control, Next). /*4*/ nasa(Start) :- go(Control, Start), write(Control), nl, fail. /*5*/ launch(Start) :- not(nasa(Start)), write('We have liftoff!').

SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 29 Sample Midterm Question: Prolog, cont’d  Rule 4’s failure causes a backtrack to match Rule 3.  Rule 3: First subgoal: Set variable Next to Start – 1 which is 9.  Rule 3: Second subgoal: Match Rule 2, set Control to 9. Rule 4: First subgoal satisfied.  Rule 4: Second subgoal: write(Control)  print “9”.  Continue printing “8”, “7”, etc. down to “0”.  When the countdown reaches 0, the cut operation in Rule 1 stops the countdown from backtracking further. ?- launch(10). /*1*/ go(0, 0) :- !. /*2*/ go(Control, Control). /*3*/ go(Control, Start) :- Next is Start - 1, go(Control, Next). /*4*/ nasa(Start) :- go(Control, Start), write(Control), nl, fail. /*5*/ launch(Start) :- not(nasa(Start)), write('We have liftoff!').

SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 30 Sample Midterm Question: Prolog, cont’d  Rule 4: Fourth subgoal causes the countdown to end in failure.  Rule 5: First subgoal has a not, and therefore that subgoal succeeds.  Rule 5: Second subgoal prints “We have liftoff!” _ /*1*/ go(0, 0) :- !. /*2*/ go(Control, Control). /*3*/ go(Control, Start) :- Next is Start - 1, go(Control, Next). /*4*/ nasa(Start) :- go(Control, Start), write(Control), nl, fail. /*5*/ launch(Start) :- not(nasa(Start)), write('We have liftoff!').