Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Today’s Topics Questions / comments? Scheme –fix the power function for negative exponents –more Scheme features Next class we'll discuss the points made in the Hughes paper

3 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 Functional Programming Scheme let's get back into scheme by fixing that power function and writing 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

4 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 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

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

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

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

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

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

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

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

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

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

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

15 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 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

16 Michael Eckmann - Skidmore College - CS 330 - Fall 2006 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!

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

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

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

20 Michael Eckmann - Skidmore College - CS 330 - Fall 2005 recall this function 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. ; this is a modified version of the one we wrote which is only valid for lists of booleans (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))

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

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

23 Michael Eckmann - Skidmore College - CS 330 - Fall 2005 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? )

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

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

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

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

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

29 Michael Eckmann - Skidmore College - CS 330 - Fall 2005 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. “LISP is worth learning for a different reason — the profound enlightenment experience you will have when you finally get it. That experience will make you a better programmer for the rest of your days, even if you never actually use LISP itself a lot. “ - ESR (Eric S. Raymond)


Download ppt "CS 330 Programming Languages 11 / 28 / 2006 Instructor: Michael Eckmann."

Similar presentations


Ads by Google