Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 330 Programming Languages 11 / 18 / 2008 Instructor: Michael Eckmann.

Similar presentations


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

1 CS 330 Programming Languages 11 / 18 / 2008 Instructor: Michael Eckmann

2 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Today’s Topics Questions / comments? Chapter 15 is required reading (covers FL's in general) --- concepts can appear on the Final and in next assignment. Same for Ch. 16 Scheme –more Scheme features –proper and improper lists nice use of a list of dotted pairs –map & apply –optional parameters

3 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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

4 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Functional Programming Scheme let's get back into Scheme by changing that power function we wrote to be able to handle negative exponents and then let's write the following functions: remove the first occurrence of an atom from a list remove all occurrences of an atom from a list replace the first occurrence of an atom in a list with another atom replace all occurrences of an atom in a list with another atom

5 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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

6 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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)‏

7 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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))‏ )‏

8 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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))‏ )‏

9 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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))‏ )‏

10 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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

11 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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)))‏

12 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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.

13 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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)‏

14 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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)‏

15 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Functional Programming Scheme –associative lists can be created by creating a list of dotted pairs –e.g. –(define cardvalues '((ace. 1) (two. 2) (three. 3) (four. 4) (five. 5) (six. 6) (seven. 7) (eight. 8) (nine. 9) (ten. 10) (jack. 10) (queen. 10) (king. 10)))‏ –we can use assoc that takes in a key and an associative list and returns the dotted pair whose car is the key –(assoc 'six cardvalues) ; would evaluate to (six. 6)‏ –also, if 'six wasn't the car of a pair then it would return #f –to get the value 6 out of there what would we do?

16 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Functional Programming Scheme –associative lists can be created by creating a list of dotted pairs –e.g. (define cardvalues '((ace. 1) (two. 2) (three. 3) (four. 4) (five. 5) (six. 6) (seven. 7) (eight. 8) (nine. 9) (ten. 10) (jack. 10) (queen. 10) (king. 10)))‏ –we can use assoc that takes in a key and an associative list and returns the dotted pair (assoc 'six cardvalues) ; would evaluate to (six. 6)‏ –to get the value 6 out of there what would we do? (cdr (assoc 'six cardvalues))‏

17 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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

18 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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

19 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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! –(p. 46,47 from Dybvig's "The Scheme Programming Language")‏

20 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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

21 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Map map –a function that takes two parameters which are a function and a list –map applies the function to each element of the list and returns a list of the returned values –the following returns (#t #t #t #f #f #f #f #t)‏ –(map number? '(1 2 3 a b r tttt 4.5))‏ –another example: –(map floor '(1 2 3 4 4.5 4.6 7.8))‏

22 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Apply apply –a function that takes two parameters which are a function and a list –it applies the function to all elements of the list and returns whatever the function would have returned if called using those elements of this list as individual parameters. –only functions that take a collection of arguments, not as a list, individually (like + and <) can be passed to apply –(apply < '(3 4 5 1)) ; ok, because (< 3 4 5 1) is a valid call. –(apply + '(4 5 6 7)) ; ok, because (+ 4 5 6 7) is a valid call.

23 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 earlier we wrote ; the list that is passed in is expected to contain only boolean values. It returns the number of #t's in the list. (define (count-t lis)‏ (cond ((null? lis) 0)‏ ((car lis) (+ 1 (count-t (cdr lis))))‏ (else (count-t (cdr lis)))‏ )‏ ;; this will return 4 because there are 4 #t's in the list (count-t '(#t #t #f #f #t #t #f))‏

24 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 How to write count-if ;; count-if is supposed to take a function and a list as parameters and count how many trues would result when the function is applied to each element of the list. (define (count-if fun lis)‏ ;; help me finish it )‏ ;; this will return 4 because 4 of the list elements are numbers (count-if number? '(1 2 3 a b r tttt 4.5))‏

25 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 How to write count-if ;; count-if is supposed to take a function and a list as parameters and count how many trues would result when the function is applied to each element of the list. (define (count-if fun lis)‏ (count-t (map fun lis))‏ )‏ ;; this will return 4 because 4 of the list elements are numbers (count-if number? '(1 2 3 a b r tttt 4.5))‏

26 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 How can we use count-if to... ;; write a function named every which takes in a function and a list as parameters and will return #t if the result of the function applied to each element is true (define (every fun lis)‏ ;; what goes here? )‏ ;; write a function named any which takes in a function and a list as parameters and will return #t if the result of the function applied to at least one element is true (define (any fun lis)‏ ;; what goes here? )‏

27 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 How can we use count-if to... ;; write a function named every which takes in a function and a list as parameters and will return #t if the result of the function applied to each element is true (define (every fun lis)‏ (= (length lis) (count-if fun lis) )‏ )‏ ;; write a function named any which takes in a function and a list as parameters and will return #t if the result of the function applied to at least one element is true (define (any fun lis)‏ (>= (count-if fun lis) 1 )‏ )‏

28 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 optional parameters A function that takes optional parameters is specified in this way: (define (fun reqp1 reqp2. optparms)‏ ; reqp1 is required ; reqp2 is required ; optparms will be a list of all the rest of the arguments passed in )‏ ; if you want a function with only optional parameters: (define (fun2. optparms)‏ ; optparms will be a list of all the arguments passed in )‏

29 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 How about writing avg... ;; write a function named avg which takes in any number of numbers as parameters and averages them. ;; how will I write the parameters when defining the function? ;; how will we handle the parameters inside the function? ;; can we use anything we just learned to allow us to write the code inside the function? (define (avg )‏ )‏

30 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 How about writing avg... ;; write a function named avg which takes in any number of numbers as parameters and averages them. ;; how will I write the parameters when defining the function? ;; how will we handle the parameters inside the function? ;; can we use anything we just learned to allow us to write the code inside the function? (define (avg. nums )‏ (cond ((null? nums) 0) ; returns 0 as the average of no nums (else (/ (apply + nums) (length nums))))‏ )‏

31 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Examples of functions as parameters So, we just witnessed several ways in which we can use functions as parameters. This is interesting since passing functions as parameters are not common in imperative languages. Hopefully you get the sense of how useful this feature is.

32 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Learning FLs There are enough people who believe, even though Scheme is not widely used, the way of thinking that is required to program in a FL like Scheme is a worthwhile skill for a computer scientist.


Download ppt "CS 330 Programming Languages 11 / 18 / 2008 Instructor: Michael Eckmann."

Similar presentations


Ads by Google