Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

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

2 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Today’s Topics Questions / comments? Scheme –Note: slides and text book show all Scheme reserved words in caps, but they need to be lowercase (at least in the environment we'll be using)‏ –search the internet for an online book called "Teach yourself Scheme in Fixnum days" and focus on the following (very short) chapters: Chapter 2: Data types including: booleans, numbers, symbols, strings, lists Chapter 3: Forms Chapter 4: conditionals especially if, cond, and, or Chapter 5 especially let and set! Chapter 6: recursion Chapter 10: alists and tables

3 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Today’s Topics I recommend downloading and installing DrScheme on your own machines. DrScheme is available on the Windows machines. It is available for Linux and is installed on the machines in the linux lab.

4 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Scheme –Primitive functions QUOTE -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))

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

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

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

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

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

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

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

12 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Functional Programming Scheme –Examples: (DEFINE (mystery m) (CAR (CDR m))) What does this function do? How is it called?

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

14 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Functional Programming Scheme –Numeric “predicate” functions return a Boolean –#T or #F –=, <>, >, =, <=, EVEN?, ODD?, ZERO? –Empty list () is considered #F –Any non-empty list is considered #T –Symbolic Atoms and Lists “predicate” functions –EQ?, LIST?, NULL?, EQUAL?

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

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

17 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Functional Programming Scheme –and, or, not –Examples: (or (= n 0) (= n 1))‏ (not (= n 0))‏ (and (eq? 'a (car lis)) (= y 1))‏

18 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Functional Programming Scheme –input / output –(read)‏ –(display "Hello World")‏ –(write "Hello World")‏ –(define x 5)‏ –(write x)‏

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

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

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

22 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Scheme Recursion –Scheme programmers typically use recursion in Scheme whenever possible

23 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 Functional Programming Example of a function containing an IF (DEFINE (factorial n)‏ (IF (= n 0)‏ 1 (* n (factorial (- n 1)))‏ )‏

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

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

26 Michael Eckmann - Skidmore College - CS 330 - Fall 2008 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) '())‏ ((EQ? atm (CAR lis)) #T)‏ (ELSE ( member atm (CDR lis)))‏ )‏

27 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 (What do I mean here?)‏

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


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

Similar presentations


Ads by Google