CS 376 Introduction to Computer Graphics 01 / 24 / 2007 Instructor: Michael Eckmann.
CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.
-
Upload
jeffery-simmons -
Category
Documents
-
view
221 -
download
0
description
Transcript of CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.
![Page 1: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/1.jpg)
CS 330Programming Languages
11 / 15 / 2007
Instructor: Michael Eckmann
![Page 2: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/2.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Today’s Topics• Questions / comments?• Chapter 6
– Pointers• solutions to memory leaks
• Functional programming – Scheme
![Page 3: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/3.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Pointers• Solutions to memory leakage problem
– Reference counters• Store a reference counter for each memory cell whose value is
the number of pointers currently pointing to it.• When pointers change value or are destroyed, the counter is
decremented. It is also checked to see if it is zero. If it is, then the memory can be reclaimed.
– Any drawbacks to this method?
![Page 4: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/4.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Pointers• Solutions to memory leakage problem
– Reference counters (continued)• Any drawbacks to this method?
– Space– Time– Circular references
![Page 5: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/5.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Pointers• Solutions to memory leakage problem (continued)
– Garbage collection• Every heap cell needs a bit or possibly a field as a flag to
indicate whether or not it contains garbage.• When want to reclaim memory the garbage collector will
– 1. Set all the flags as “containing garbage”– 2. Go through the program and determine all the memory
that is being pointed to --- and changes the flags for those cells as “not containing garbage”
– 3. All the ones still marked as “containing garbage” are reclaimed.
– All preceding discussion assumed that the heap cells were all the same size. Major difficulties arise when non-uniform size cells are used. Why?
![Page 6: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/6.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Functional Programming• Read the paper “Why Functional Programming Matters” (1984)
by John Hughes found here:• http://www.md.chalmers.se/~rjmh/Papers/whyfp.html• (contains a link to a pdf of the paper.)• Please read before Tuesday the 20th so we can discuss it together
as a class along with the first 5 sections of chapter 15.• Suggestions on reading this paper (or any technical paper)
– If you don't understand something after a little thought, that's o.k. continue on to get the gist of the whole thing then, come back to what wasn't understood.
![Page 7: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/7.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Functional Programming• Let's look at a webpage that contains some good bullet points
regarding functional programming in general.
![Page 8: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/8.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Scheme• Scheme
– Static scoping exclusively– Small size– Functions are first-class citizens
• Can be values of expressions• Can be elements of lists• Can be assigned to variables• Can be passed as parameters
– small, simple syntax --- consistent syntax– simple semantics
![Page 9: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/9.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Scheme• Note that the textbook uses capital letters
for the keywords in the language and we will do the same on the slides, but when we code using DrScheme this is not necessary.
![Page 10: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/10.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Scheme• Scheme
– Expressions are evaluated by the function EVAL
– Literals evaluate to themselves– Function calls are evaluated by
• First evaluate all the parameter expressions• Then evaluate the function after the values of the
parameters are known• The value of the last expression in the body is the
value of the function (sound familiar?)• All but the last should be familiar to imperative
programmers.
![Page 11: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/11.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Scheme• Scheme
– Primitive functionsArithmetic: +, -, *, /, ABS, SQRT, REMAINDER, MIN, MAX
e.g., (+ 5 2) yields 7
what would (- 24 (* 5 3)) yield?• If * is given no parameters, it returns 1
(multiplicative identity.)• If + is given no parameters, it returns 0 (additive
identity.)
![Page 12: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/12.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Scheme• Scheme
– Primitive functions• If – is given more than two parameters, it acts as if
the second through the last are summed and this sum is subtracted from the first.
• If / is given more than two parameters, it acts as if the second through the last are multiplied together and this product is divided into the first.
![Page 13: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/13.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Scheme• Scheme
– Primitive functionsQUOTE -takes one parameter; returns the parameter
without evaluation • QUOTE is required because the Scheme interpreter,
named EVAL, always evaluates parameters to function applications before applying the function. QUOTE is used to avoid parameter evaluation when it is not appropriate
• QUOTE can be abbreviated with the apostrophe prefix operator
e.g., '(A B) is equivalent to (QUOTE (A B))
![Page 14: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/14.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Scheme• Scheme
– If you wanted a list with the symbols / 8 4 in that order and you did this:
(/ 8 4)• Scheme would evaluate that as a function and the
result will be 2– So, to have the list not be evaluated, use the QUOTE:
'(/ 8 4) • Scheme gives us the list with those three elements;
no evaluation occurs.• alternatively (QUOTE (/ 8 4))
![Page 15: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/15.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Scheme• Scheme
– (DEFINE hello 5.6)– If you wanted the symbol hello instead of the value in
some constant named hello'hello vs.hello
– The first one gives us hello– The second one evaluates to 5.6
![Page 16: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/16.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Scheme• Scheme
CAR takes a list parameter; returns the first element of that list
e.g., (CAR '(A B C)) yields A (CAR '((A B) C D)) yields (A B)
CDR takes a list parameter; returns the list after removing its first element
e.g., (CDR '(A B C)) yields (B C) (CDR '((A B) C D)) yields (C D)
![Page 17: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/17.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Scheme• Scheme
CONS takes two parameters, the first of which can be either an atom or a list and the second of which is a list; returns a new list that includes the first parameter as its first element and the second parameter as the remainder of its result
e.g., (CONS 'A '(B C)) returns (A B C) (CONS '(A B) '(B C)) returns ((A B) B C)
LIST - takes any number of parameters; returns a list with the parameters as elements
![Page 18: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/18.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Scheme• Scheme
– DEFINE – used to create programmer-defined functions / or bind names to values of expressions
– When names are bound to values of expressions they are NOT variables, instead they are named constants.
– Either two atoms as parms, or two lists.– Example when two atoms are given (creates a named
constant): (DEFINE games_in_season 162)
![Page 19: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/19.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Scheme• Scheme
– DEFINE – used to create programmer-defined functions / or bind names to values of expressions
– The form when two lists are given • binds the expressions collectively as a function to a
function name and its parameters: (DEFINE (func_name parameters)
expression {expression})
– Examples on next slide
![Page 20: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/20.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Scheme• Scheme
– Examples:(DEFINE (square x) (* x x))
(DEFINE (hypotenuse side1 side1)
(SQRT (+ (square side1) (square side2))) )
What would these look like as imperative language functions?
![Page 21: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/21.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Functional Programming• Scheme
– Examples:(DEFINE (mystery m) (CAR (CDR m)))
What does this function do?
How is it called?
![Page 22: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/22.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Functional Programming• Scheme
– Examples:(DEFINE (mystery m) (CAR (CDR m)))
What does this function do?CDR returns the list remaining after first element
removedCAR returns the first element of a listso, a CAR of a CDR is the second element
How is it called?(mystery '(W X Y Z))
![Page 23: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/23.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Functional Programming• Scheme
– Numeric “predicate” functions return a Boolean – #T or #F– =, <>, >, <, >=, <=, EVEN?, ODD?, ZERO?– our textbook says that empty list () is considered #F
and any non-empty list is considered #T (this differs from other Scheme documentation which says #F is false, and all other objects are treated as true (e.g. (), “false”, 3, nil are all considered true)
– Symbolic Atoms and Lists “predicate” functions– EQ?, LIST?, NULL?, EQUAL?
![Page 24: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/24.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Functional Programming• Scheme
– There is a difference between• =• eq?• equal?
– = is used for numeric comparison– eq? is used for symbol/atom comparison– equal? is used for symbol/atom or list comparison
• There's more to it than what I say above ---- I'll photocopy some examples and their results for eq?, equal? and eqv? for your edification.
![Page 25: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/25.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Functional Programming• Scheme
– =, <>, >, <, >=, <=, EVEN?, ODD?, ZERO?– EQ?, LIST?, NULL?, EQUAL?
– Examples:(= n 0)(NULL? somelist)(EQ? 'A (CAR somelist))(LIST? '() )(EQUAL? '(a b z) '(a b z))
![Page 26: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/26.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Functional Programming• Scheme
– and, or, not
– Examples:(or (= n 0) (= n 1))(not (= n 0))(and (eq? 'a (car lis)) (= y 1))
![Page 27: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/27.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Functional Programming• Scheme
– input / output– (read)– (display "Hello World")– (write "Hello World")– (define x 5)– (write x)
![Page 28: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/28.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Functional Programming• Scheme
– IF takes three parms(IF predicate then_expression else_expression )
– COND – is like switch / case statements(COND
( predicate1 expression { expression } )( predicate2 expression { expression } )...( predicaten expression { expression } )( ELSE expression { expression } )
)
![Page 29: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/29.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Functional Programming• COND
– Predicates are evaluated in order until one is #T– Then the expressions that follow that first #T predicate
are evaluated and the value of the last one evaluated is the returned value.
– If no predicate is #T, then COND returns (), the empty list.
– ELSE at the end acts like a default if none of the other predicates are true it will evaluate that one.
– Can you see any differences or similarities to switch statements in Java and C++?
![Page 30: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/30.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Functional Programming• COND
– Can you see any differences or similarities to switch statements in Java and C++?
– In COND, there are implied “breaks” after each predicate (case.)
![Page 31: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/31.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Scheme• Recursion
– Scheme programmers typically use recursion in Scheme whenever possible
![Page 32: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/32.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Functional Programming• Example of a function containing an IF
(DEFINE (factorial n)(IF (= n 0)
1(* n (factorial (- n 1)))
))
![Page 33: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/33.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Functional Programming• Example of a function containing a COND
(DEFINE (compare x y) (COND
( (> x y) ( DISPLAY “x is > y” ))( (< x y) ( DISPLAY “x is < y” ))( ELSE ( DISPLAY “x is = y” )
))
![Page 34: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/34.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Functional Programming• Want to try to write a function that determines whether an
atom is in a particular list? e.g. (member 'B '(A B C)) should return #T and (member 'X '(A B C)) should return #F.
![Page 35: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/35.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Functional Programming• Want to try to write a function that determines whether an
atom is in a particular list? e.g. (member 'B '(A B C)) should return #T and (member 'X '(A B C)) should return () (which is #F.)
(DEFINE (member atm lis) (COND
((NULL? lis) #F)((EQ? atm (CAR lis)) #T)(ELSE ( member atm (CDR lis))))
)
![Page 36: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/36.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Schemewhen writing functions dealing with a list lis, if you want to go through the elements one at a time, use (car lis) to get the first and recurse on (cdr lis) also, you'll need to tell your function when to stop, that is, when the list is null, so (null? lis) should be the first thing you check.In general recursive functions need to
1st check the base case (the case that will be true when the recursion should stop)
and in the recursive step(s) make sure that the function is working towards the base case (What do I mean here?)
![Page 37: CS 330 Programming Languages 11 / 15 / 2007 Instructor: Michael Eckmann.](https://reader035.fdocuments.us/reader035/viewer/2022062401/5a4d1b0e7f8b9ab05998d65e/html5/thumbnails/37.jpg)
Michael Eckmann - Skidmore College - CS 330 - Fall 2007
Scheme• Let's run DrScheme and try some things out. While we're
coding, as I introduce any new built-in functions or anything we haven't seen before I'll explain what they are.
• I'll then save the code and post it.