CS 152 Programming Language Paradigms March 12 Class






























- Slides: 30
CS 152: Programming Language Paradigms March 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak www. cs. sjsu. edu/~mak
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 2
Screenshot of the original Smalltalk environment. SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 3
Smalltalk Messages o o Unary message: A message with no arguments. Keyword message: A message that expect arguments. n n The message selector (method name) ends in a colon. Example: Set new includes: 'Hello' o o o 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. n Example: a at: i+1 put: (a at: i). n at: put: is the complete message selector. SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 4
Smalltalk Messages, cont’d o Binary messages allow you to write arithmetic and comparison expressions with infix notation. n Examples: 3 + 4 3 < 4 SJSU Dept. of Computer Science Spring 2014: March 12 "Returns 7" "Returns true" CS 152: Programming Language Paradigms © R. Mak 5
Smalltalk Variables o Use variables to refer to objects. n Example: The assignment operator is : = or o Temporary variables are declared between vertical bars n o They are not capitalized. Smalltalk variables have no assigned data type n A variable can name any thing. SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 6
Smalltalk Variables, cont’d o o Statements are separated by periods. A sequence of messages to the same object are separated by semicolons: n o Example: Smalltalk variables use reference semantics, not value semantics. n 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 7
Smalltalk Equality and Identity Operators o o 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 8
Smalltalk Statements o The to: do message creates a loop. n o Even control statements are expressed with message passing. Enclose a code block with square brackets [ ] n n 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 9
Smalltalk Statements, cont’d o An if. True: if. False message expresses a choice of action. o Print the contents of an array: "Print to the transcript each element followed by a carriage return. " array do: [: element | Transcript next. Put. All: element print. String; cr] SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 10
Review for the Midterm o A test of understanding, not memorization. o Open book, open notes, open laptop, open Internet. n n 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 11
Review for Midterm, cont’d o Historical overview n n n o von Neumann architecture Machine and assembly languages FORTRAN COBOL Algol Pascal Abstractions n n n Data Control Basic Structured Unit SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 12
Review for Midterm, cont’d o Paradigms n n n o o o 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 13
Review for Midterm, cont’d o Good language design n n n Efficiency Regularity Generality Orthogonality Uniformity Security Extensibility _ SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 14
Review for Midterm, cont’d o Functional programming n n n o Programs as functions Recursion Referential transparency Lack of state First-class objects Higher-order functions Scheme n n 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 15
Review for Midterm, cont’d o Scheme, cont’d n Procedures and predicates Conditional expressions n Recursion n o o flat mutual deep tail recursion n Symbol manipulation Symbolic differentiation n Scope n o o local bindings closure SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 16
Review for Midterm, cont’d o Scheme, cont’d n Procedure as a first-class object o o n as an argument as a return value Metalinguistic power o o eval apply _ SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 17
Review for Midterm, cont’d o Logic n o Deductive reasoning Prolog n Declarative language Objects and relations Fact, rules, and questions Conjuctions Variables n Backtracking n n o n n n place markers Resolution Unification Cut SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 18
Review for Midterm, cont’d o Object-oriented programming n n n n 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 19
Review for Midterm, cont’d o Smalltalk n n Purely object-oriented Objects Messages IDE _ SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 20
Sample Midterm Question: Scheme o 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. n n If there is a tie, it moves only one of the minimum elements. Example: (define nums '(4 3 2 1 0 1 8 0 2 9)) (min-to-head nums) (0 4 3 2 1 1 8 0 2 9) SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 21
Sample Midterm Question: Scheme, cont’d o 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. n Example: (sort nums) (0 0 1 1 2 2 3 4 8 9) SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 22
Sample Midterm Question: Scheme, cont’d n n 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. (define sort (lambda (lst) (if (null? lst) '() (let ((mth-lst (min-to-head lst))) (cons (car mth-lst) (sort (cdr mth-lst)))) n This is a selection sort. SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 23
Sample Midterm Question: Scheme, cont’d o Write a recursive Scheme procedure min-to-head. n o 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. o o 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. o 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 CS 152: Programming Language Paradigms 24 Spring 2014: March 12 © R. Mak )))
Sample Midterm Question: Scheme, cont’d o Assume that min-to-head has correctly worked on the cdr of the list. n o o 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. n If necessary, swap them. n 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 25
Sample Midterm Question: Prolog o Given this Prolog database and the query: /*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). n Explain the result of the query in terms of resolution, unification, and backtracking. SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 26
Sample Midterm Question: Prolog, cont’d /*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). o Answer: o o o 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. _ SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 27
Sample Midterm Question: Prolog, cont’d /*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). o Rule 5: First subgoal: nasa(10) matches Rule 4. o o Rule 4: First subgoal: Match Fact 2. o o o Variable Start is bound to 10. 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. SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 28
Sample Midterm Question: Prolog, cont’d /*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). o Rule 4’s failure causes a backtrack to match Rule 3. o o n o o o 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. SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 29
Sample Midterm Question: Prolog, cont’d /*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!'). o o o 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!” _ SJSU Dept. of Computer Science Spring 2014: March 12 CS 152: Programming Language Paradigms © R. Mak 30