55
CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Embed Size (px)

Citation preview

Page 1: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

CS 330Programming Languages

11 / 14 / 2006

Instructor: Michael Eckmann

Page 2: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Today’s Topics• Questions / comments?• Look for homework assignment via email by tomorrow. To

be due Tuesday night at 11:59 pm.• Solution to palindrome in Perl• Chapter 6

– multidimensional arrays

– associative arrays / hashes

– records

– pointers

• Start Scheme

Page 3: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Coming up next ...• Scheme and Functional Programming in general is the

next topic after we finish chapter 6.

Page 4: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Multidimensional Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

• How to access 2-dimensional array elements at run-time (assuming row major order and all rows have same number of columns and index starts at 0).

– We have the subscripts to look for

– We need address of first element

– What else do we need?

Page 5: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Multidimensional Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

• How to access 2-dimensional array elements at run-time (assuming row major order and all rows have same number of columns and index starts at 0).

– We have the subscripts to look for

– We need address of first element

– We need the size of one element (determined from the type of the array.)

– We need to know the number of columns per row

– Luckily all this info is stored in the descriptor for the array

Page 6: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Multidimensional Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

• Address of element at row i, column j is =

address_of_array +

(num_cols * i + j) * size_of_element;

Page 7: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Associative Arrays

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

• Unordered collection of data indexed by keys.

• Each element of an associative array is a pair:

– key, value

• The reason Perl's associative arrays are called hashes is because they are implemented using a hash table and hash function.

• Because the data is unordered, Perl can store it in memory in any order it cares to.

• Can someone define a hash table?

Page 8: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Hashing

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

• A hash table can be implemented as a 1d array of linked lists.

• Each key is supplied to a hash function which returns a number.

• The index to the array is determined by the number returned from the hash function mod array_length.

• Then go sequentially through the linked list in that element of the array to find the value.

• The length of the array is chosen wisely (usually a prime number so the mod works well) and the hash function is chosen wisely (to distribute the keys well throughout the hash table.)

Page 9: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Records• Records are a way to hold different types of data

as a group. e.g. A record might hold an employee name, salary, hire-date, etc.

• The individual elements of a record are called fields.

• Design issues include:–How are fields referenced in the language–How are fields within nested record types

referenced in the language

Page 10: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Records• COBOL has elaborate record structures.

• A record name is defined at a LEVEL with a low number. Fields are defined at a LEVEL with a higher number. Nested Records are created by having LEVELs in between these.

• We'll go over an example of COBOL records in the next couple of slides

• struct defines records in C, C++ and C#

• What defines records in Java?

Page 11: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Records• COBOL record example.01 STUDENT-RECORD.

05 STUDENT-NAME.

10 LAST-NAME PICTURE X(20).

10 FIRST-NAME PICTURE X(20).

05 BIRTH-DATE PICTURE 999999.

05 EXPECTED-GRAD PICTURE 9999.

• Note that STUDENT-NAME is also a RECORD.

Page 12: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Records• COBOL record example.01 EMPLOYEE-RECORD.

05 EMPLOYEE-NAME.

10 LNAME PICTURE X(20).

10 FNAME PICTURE X(20).

05 BIRTH-DATE PICTURE 999999.

05 SALARY PICTURE 99999V99.

Page 13: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Records• COBOL has a MOVE CORRESPONDING

command to copy values of fields of one record to another. It only copies values of fields with the same name in both records.

• Do you see any benefits or drawbacks to this?

Page 14: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Records• Ada also allows nested levels of records.

• Do C++ or Java allow nested records?

• Referencing field names–Fully qualified reference–Elliptical reference

LAST-NAME OF STUDENT-RECORD

• How do we reference fields in Java or C++?

Page 15: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Records• There are some similarities and differences between records

and arrays.

– Records are used when the data is of different types

– Arrays are used when the data is all the same type

– Record elements are referenced with static field names as “subscripts.”

– Array elements are referenced with dynamic subscripts.

• Implementation

– In the descriptor, the address of the record is stored as well as the name, type and offset address of each field.

Page 16: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Unions & Records• Unions may be decided to be allowed in records or not.

– what are reasons for and against allowing Unions in records

Page 17: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Pointers• Pointer types

– Store memory addresses or nil (no address.)

• For indirect addressing

• For dynamic storage management

– Can access data at a particular memory address in the heap

• Provide a way to have data structures that shrink and grow during execution.

Page 18: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Pointers• Using pointers

– Pointer reference (the address)

– Indirect reference (aka dereference) gives us what data is in the memory address stored in the pointer

• Dereferencing is usually explicit (as in C, C++ with the * operator) but it is sometimes implicit (ala Ada)

• Dereferencing example

– variable ptr is a pointer and contains the memory address 7080

– Memory address 7080 contains the integer value 206

– j is a variable of type integer.

Page 19: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Pointers int j;

int *ptr;// code missing

// to assign val to ptr

j = *ptr;// line above assigns

// value in address

// that ptr points to

Page 20: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Pointers• Accessing fields of records via pointers to records

– C++ (pointer to a struct)

– (*ptr).field_name (* dereferences, . accesses field )

– ptr -> field_name ( -> does both )• Because this is a common operation, there's a

shorthand

– Ada

– ptr.field_name • (because of implicit dereferencing acts just like -> in

C++)

Page 21: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Pointers• Problems

– Dangling pointer (or dangling reference in those languages that don't have pointers)

– Memory leakage

– Did anyone read about these in the text or know what they are from prior knowledge?

Page 22: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Pointers• Problems

– Dangling pointer (or dangling reference in those languages that don't have pointers)

• When a pointer contains the address of a variable that was deallocated already

• example:– pointer p1 points to some heap dynamic variable– pointer p2 = p1 // make p2 also point there– deallocate p1's heap dynamic variable– // now, p2 is a dangling pointer

– If programmer cannot explicitly deallocate heap dynamic variables then there will be no possibility of dangling pointers.

Page 23: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Pointers• Problems

– Memory leakage

• When a pointer's value changes (that is a different address is stored in it) and the address it was pointing to didn't have it's space deallocated

• Garbage collection (frees deallocated memory for use)– C++ (done explicitly by programmer)– Java (done automagically)

Page 24: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Pointers• Let's look at these problems in Ada, C/C++, Fortran 95, Java

• Ada

– Pointers are of type access

– Handles the dangling pointer problem by design

• Implicit deallocation of memory at end of scope is done

• Also allows explicit deallocation by the programmer via the deallocator: Unchecked_Deallocation

(which can cause the dangling pointer problem)

– Memory leakage

• Nothing by design to prevent this

Page 25: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Pointers• Let's look at these problems in Ada, C/C++, Fortran 95, Java

• C/C++

– Both the dangling pointer problem and memory leakage exist in these languages

– Can do pointer arithmetic

– Can have pointers to any type and pointers to functions

– One use of pointers is to pass variables by reference (so they may be changed within the function) --- contrast with pass by value, where the variable's value is copied to new temporary space in the function.

– & is used to get the address of a variable

– Examples of this stuff ...

Page 26: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Pointers• C/C++

int *ptr;

int count=30, init=70;

ptr = &init;

count = *ptr;

What does this code do?

Page 27: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Pointers• C/C++

int *ptr; // declare a pointer to an int

int count=30, init=70;

ptr = &init; // store the address of init in ptr

count = *ptr;

// dereference ptr and store what's in the address

// that ptr is pointing to

// so what value does count have?

Page 28: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Pointers• C/C++ (pointer arithmetic)

int *ptr; // declare a pointer to an int

int grades[ 100 ];

ptr = grades; // grades is a “pointer” to grades[0] and now

// so is ptr

*(ptr + 1)

// here the + is pointer addition, so it actually adds

// the size of one int to ptr so that it dereferences

// (ptr + 1) to grades[1]

ptr[index]; // can use ptr like an array.

// What is the difference between ptr and grades?

Page 29: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Pointers• Let's look at these problems in Ada, C/C++, Fortran 95, Java

• Fortran95

– Has dangling pointer problem because allows explicit Deallocation command on a pointer

• how does that make dangling pointers possible?

– Like Ada, pointers are implicitly dereferenced, but Fortran95 also provides a way to explicitly not dereference a pointer

• What does this mean again?

• Why might this be?

Page 30: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Pointers• Let's look at these problems in Ada, C/C++, Fortran 95, Java

• Java

– Anybody know whether we have dangling pointers/references or memory leakage problems in Java?

Page 31: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Pointers• Reference types in C/C++ and Java and C#

– C/C++ reference type is a special kind of pointer type

• Used when declaring parameters to a function so that it is passed by reference

• Formal parameter is specified with an &

• But inside the function, it is implicitly dereferenced.– Makes code more readable and safer

• Can do same thing with regular pointers but code is less readable (b/c explicit dereferencing required) and less safe (b/c one can alter the address)

Page 32: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Pointers• Reference types in C/C++ and Java and C#

– C/C++ reference type is a special kind of pointer type

• Would there be any use for passing by reference using a constant reference parameter (that is, one that disallows its contents to be changed)?

– Java

• References replace C++'s pointers– Why?

• Java references refer to class instances (objects), so arithmetic with them doesn't make sense.

• No dangling references b/c implicit deallocation

Page 33: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Pointers• Reference types in C/C++ and Java and C#

– C#

• Has both Java-like references and C++-like pointers.

• Best (or worst) of both worlds?

• Pointers are discouraged --- methods that use pointers need to be modified with the unsafe keyword.

Page 34: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Pointers• Implementation of pointers

– Pointers hold an address

• Solutions to dangling pointer problem– Tombstones

• A tombstone points to (holds the address of) where the data is (a heap-dynamic variable.)

• Programmer specified pointers can only point to a tombstone (which in turn points to the actual data.)

• When deallocate, the tombstone is set to nil.

• What does this solve?

• Why aren't they used do you think? --- Know of any languages that use them?

Page 35: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Pointers• Solutions to dangling pointer problem (continued)

– locks-and-keys

• Pointers and variables need different implementation for this method

• Pointers are ordered pairs of an integer key and an address.

• Heap-dynamic variables – include a header cell that stores a lock value – and storage cell(s) for the variable itself

• During allocation a lock value is calculated and placed in the key portion of the pointer AND the header cell of the variable.

• Key and header cell are compared when the pointer is dereferenced and if they're the same it's a legal reference otherwise it is an illegal reference (which causes run-time error.)

Page 36: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Pointers• Solutions to dangling pointer problem (continued)

– Locks-and-keys (continued)

• Multiple pointers may point to the variable but they all must have the same key.

• When variable is deallocated (explicitly), the variable's header cell is changed to an illegal lock value (so no key will match it ever.)

– Any other solutions you can think of to handle the dangling pointer problem?

Page 37: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Pointers• Solutions to dangling pointer problem (continued)

– Any other solutions you can think of to handle the dangling pointer problem?

• Don't allow programmer to explicitly deallocate heap-dynamic variables. Like Java and LISP. Also like C#'s references (but not like C#'s pointers.)

Page 38: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

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 39: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Pointers• Solutions to memory leakage problem

– Reference counters (continued)

• Any drawbacks to this method?– Space– Time– Circular references

Page 40: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

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 41: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Functional Programming• Read the paper “Why Functional Programming Matters” 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 our next class meeting so we can discuss it together as a class.

• 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 42: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

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

– “simple” syntax --- more correct to say consistent?

– “simple” semantics

Page 43: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

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 44: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Scheme• Scheme

– Primitive functions

Arithmetic: +, -, *, /, 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 45: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

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 46: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

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 47: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Scheme• Scheme

– If you wanted a list with the symobls / 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 48: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

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 49: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

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 50: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

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 51: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

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 52: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

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 53: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

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 54: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Functional Programming• Scheme

– Examples:(DEFINE (mystery m) (CAR (CDR m)))

What does this function do?

How is it called?

Page 55: CS 330 Programming Languages 11 / 14 / 2006 Instructor: Michael Eckmann

Michael Eckmann - Skidmore College - CS 330 - Fall 2006

Functional Programming• Scheme

– Examples:(DEFINE (mystery m) (CAR (CDR m)))

What does this function do?

CDR returns the list remaining after first element removed

CAR returns the first element of a list

so, a CAR of a CDR is the second element

How is it called?

(mystery '(W X Y Z))