Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.

Similar presentations


Presentation on theme: "CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann."— Presentation transcript:

1 CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann

2 Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Today’s Topics Questions / comments? Scheme –multiple topics

3 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))) )

4 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” ) )

5 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.

6 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))) )

7 Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Scheme when 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?)

8 Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Scheme (review of some funcs) car --- of a list, returns the first element of a list --- it is only defined for non-empty lists cdr (pronounced could-er) --- of a list, returns the remaining elements of the list as a list, excluding the first element --- it is only defined for non-empty lists cons --- takes two arguments, the second must be a list --- returns a new list with the first argument being the first element of the new list and the elements of the second argument list being the remaining elements of the new list

9 Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Scheme (review of some funcs) null? -- returns #t if the argument is the empty list -- returns #f otherwise list? -- returns #t or #f depending on whether the argument is a list or not (cond ( predicate1 expression { expression } ) ( predicate2 expression { expression } )... ( predicaten expression { expression } ) ( else expression { expression } ) )

10 Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Scheme (review of some funcs) eq? -- used to compare atoms for equality = -- used to compare numbers for equality equal? -- used to compare lists for equality #t is literal true #f is literal false

11 Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Functional Programming Scheme let's write the following functions: determine if an atom is in a list or any sublist of a list (deep member test) determine if a list is made up of only atoms (that is, no sublists) power function (for positive exponents) power function (for positive or negative exponents)

12 Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Functional Programming Scheme let's write the following functions: remove the first occurence of an atom from a list remove all occurences of an atom from a list replace the first occurence of an atom in a list with another atom replace all occurences of an atom in a list with another atom

13 Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Functional Programming Scheme logical functions –and or not some more functions available in scheme: –number? list? boolean? for numbers: –real? integer? rational? complex? odd? even? –+, -, *, /, abs, sqrt, remainder, min, max, round, truncate, ceiling, floor some functions on lists –length, reverse –list-ref

14 Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Functional Programming Scheme examples: –(define mylist '(a b c)) –mylist ; gives (a b c) –(list-ref mylist 2) ; gives c –(length mylist) ; gives 3 –(reverse mylist) ; gives (c b a), but mylist is still (a b c) reverse is not a deep reverse (i.e. it does not reverse any sublists)

15 Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Functional Programming Scheme –lambda creates a nameless function –e.g. (lambda (a) (* a a)) ; this is the lambda function with ; a as a parameter and it squares a ((lambda (a) (* a a)) 4) ; how to call it –to name the function we can do: (define square (lambda (a) (* a a)) )

16 Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Functional Programming Scheme –to name the function we can do: (define square (lambda (a) (* a a)) ) –instead we have been using the shortened form to define functions: (define (square a) (* a a)) )

17 Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Functional Programming more generally –to name the function we can do: (define myname (lambda (parameters) (expression)) ) –instead we have been using the shortened form to define functions: (define (myname parameters) (expression)) )

18 Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Functional Programming Scheme –recall how I said cons takes an atom and a list and returns the new list with the atom on the front –and car returns the first element of a list –and cdr returns the remainder of the list as a list –This is true for what are called PROPER LISTS

19 Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Functional Programming Scheme –PROPER LISTS are those lists whose last cdr is the empty list. –IMPROPER LISTS are those lists whose last cdr is something other than the empty list –see handout (p. 18,19 from Dybvig's "The Scheme Programming Language") –Anything created by consing two things together is a pair –(cons 'a 'b) ; creates a dotted pair (an improper list) –(cons 'a '(b)) ; creates a proper list (which is also a pair) –(cons 'a (cons 'b '(c)))

20 Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Functional Programming Scheme –PROPER LISTS are displayed in parens with a space between the elements e.g. (define proplist (cons 'a (cons 'b '(c)))) proplist ; displays as (a b c) –but more explicitly that list (a b c) can be thought of as: (a. (b. (c. ()))) –where it is a nested set of pairs with dots between the car and the cdr of the pair –Notice that the last cdr is the empty list.

21 Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Functional Programming IMPROPER LISTS are displayed with dots. –e.g. (define improplist (cons 'a 'b)) improplist ; displays as (a. b) (define improplist2 (cons 'a (cons 'b 'c)) improplist2 ; displays as (a b. c) but more explicitly improplist2 can be thought of as (a. (b. c)) (a b. c) is shorthand for that (define improplist3 (cons (cons 'a 'b) 'c) improplist3 ; displays as ((a. b). c)

22 Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Functional Programming Scheme –list? ; determines if the argument is a proper list –pair? ; determines if the argument is a pair (list? '()) returns #t but (pair? '()) returns #f list? and pair? will return #t for non-empty proper lists list? will return #f for improper lists pair? will return #t for improper lists list? and pair? will return #f for non-pairs like –(pair? 'a) –(list? 'a)

23 Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Functional Programming Scheme –let creates local variables Syntax: (let ((var val)...) exp1 exp2...) e.g. (let ((x 5) (y 7)) (+ x y)) ; x and y are local (let ((z (* 6 6))) (+ z z)) ; z is local without let we would write: (+ (* 6 6) (* 6 6)) ; and the * would be performed twice

24 Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Functional Programming Scheme –let (let ((x 2)) (display (+ x 3)) (display "\n") (display x) ) ; x's value is not changed throughout the code above except ; when it is first given the value 2 (display x) ; x is local to the above code so this fails

25 Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Functional Programming Scheme –set! performs assignments on previously declared variables not a pure functional language feature let's look at the function on the handout written with and without set!

26 Michael Eckmann - Skidmore College - CS 330 - Fall 2007 Functional Programming Scheme –let's write some more functions –count the number of #t's in a list of booleans (count-t '(#t #t #f #f #t #t #f)) ; should return 4


Download ppt "CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann."

Similar presentations


Ads by Google