37
CS 1321 1321

CS 1321. CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 10 Sept 25th, 2001 Fall Semester

Embed Size (px)

Citation preview

CS13211321

CS1321:Introduction to Programming

Georgia Institute of Technology

College of Computing

Lecture 10

Sept 25th, 2001

Fall Semester

Today’s Menu

A review of Insertion

Insertion Sort

Intermezzo II and Intermediate Level

Last Time…

Last time we were dealing

with the problem of organizing

a list of complex data types

(structures) and the list manipulation involved with

that process…

Our particular data structure was a TA record, which

stored within it a name, a salary and a number of

hours worked:

(define-struct ta-record (name salary hours))

;; A ta-record is a structure: ;; (make-ta-record a b c) where a is a symbol;; and b & c are numbers

Last Time…

Further, we were working with

a list of ta-records:

;; a list-of-tas is either:

;; 1) the empty list, empty, or

;; 2) (cons t lot) where t is a TA-record, and lot

;; is a list-of-tas

Inserting items…

One of the topics we discussed in the last lecture was inserting a new item into a list that stores that type of item. As we discovered, there were three “categories” of insertion:

1 2 3 empty

At the beginning

At the end

At a particular location (the middle)

The Code…One of the tasks we examined last time was inserting a new TA record into a list of TA records. This list has the property that each TA on the list has a salary that is higher than the TA that preceded it in the list, and lower than the value that follows (ascending order).

Using our template for recursion on a list of TAs, and our template for accessing data, we constructed some functions to accomplish this fact.

(define (insert new-ta ta-list)

(cond ((empty? ta-list) (cons new-ta ta-list))

(else (cond ((paid-more? new-ta (first ta-list))

(cons (first ta-list)

(insert new-ta (rest ta-list))))

(else

(cons new-ta ta-list))))))

(define (paid-more? TAone TAtwo)

(> (ta-record-salary TAone)

(ta-record-salary TAtwo)))

Does this work?

empty‘bob ‘bubba ‘billy

5.5 6.5 7.5

300 300 300

Does this work?

empty‘bob ‘bubba ‘billy

5.5 6.5 7.5

300 300 300

‘bryan

4.5

300

Does this work?

empty‘bob ‘bubba ‘billy

5.5 6.5 7.5

300 300 300

‘bryan

4.5

300

Does this work?

‘bryan

4.5

300

empty‘bob ‘bubba ‘billy

5.5 6.5 7.5

300 300 300

Does this work?

empty‘bob ‘bubba ‘billy

5.5 6.5 7.5

300 300 300

‘bryan

7.0

300

Does this work?

empty‘bob ‘bubba ‘billy

5.5 6.5 7.5

300 300 300

‘bryan

7.0

300

This isn’t right…(cons first of the listonto recursive callwith rest of the list)

This isn’t right…(cons first of the listonto recursive callwith rest of the list)

Does this work?

empty‘bob ‘bubba ‘billy

5.5 6.5 7.5

300 300 300

‘bryan

7.0

300

NO! Keep going!

Does this work?

empty‘bob ‘bubba ‘billy

5.5 6.5 7.5

300 300 300

‘bryan

7.0

300

Does this work?

empty‘bob ‘bubba ‘billy

5.5 6.5 7.5

300 300 300

‘bryan

7.0

300

And to cut a long story short…

empty

‘bob ‘bubba ‘billy

5.5 6.5 7.5

300 300 300

‘bryan

8.0

300

Our Task…As explained last time, our overall goal was to create a function sort that took in a list of TAs of undetermined length, and sort that list of TAs in ascending order by TA salary.

We would take in as input a list of TAs. To simplify the matter, we should return as output a NEW list of TAs, which contains all the same information as the original list, but this time in Sorted order.

So How Do We Work this?

Let’s look at our template again…

(define (process-list-of-tas in-list)

(cond ((empty? in-list) …)

(else … (first in-list) …

… (process-list-of-tas (rest

in-list))…

)))

Let’s change our template

We’ll do the easy parts…

(define (sort in-list)

(cond ((empty? in-list) in-list)

(else … (first in-list) …

… (sort (rest in-list))…

)))

Let’s change our template

We’ll do the easy parts…

(define (sort in-list)

(cond ((empty? in-list) in-list)

(else … (first in-list) …

… (sort (rest in-list))…

)))

If our list is empty,it’s already sorted

Now’s the sticky part…

(else … (first in-list) …

… (sort (rest in-list))…

What do we want to do if our list is not empty?

What do we know?

1) We can only “deal” with the first item directly

2) We’re writing a function sort which will sort a list…

What else?

Well, we just wrote a function insert which has the ability to insert a SINGLE ta-record into a list of sorted ta-records and maintain the sorted order of the list…

So if we let the recursive call handle the bother of sorting the rest of the list….

We find our solution

(define (sort in-list)

(cond ((empty? in-list) in-list)

(else (insert (first in-list)

(sort (rest in-list))))))

Congratulations!

You’ve just written a version of insertion-sort!

<test it out! Use the code provided in lecture10.scm!>

(We’re going to be testing it with an insertion-sort on a list that only takes numbers)

For more examples, read chapter 12!

Intermezzo II

Hooray! We’re no longer beginners!

Ladies and Gents, believe or not, you’re no longer beginners. You’ve had some experience with Scheme, and it’s time for you to step up to the next level of scheme: Intermediate

So what’s changed?

Click here for A Full Comparison Between Language Levels

But biggest impact this switch in language has on you guys is when we deal with Lists…

Before and After

Beginner

Becomes

Intermediate

Intermediate Mode Lets Us Abbreviate

No longer do we have to type in:

(cons a (cons b (cons … (cons z empty)…)))

to produce a list. We now assume you understand a list is composed of cons cells. So we abbreviate:

(list a b … z)

(list ‘a ‘b‘c)

A list is made of cons cells. It’s understood…so

why write it?

This is the list consisting of the symbols a, b, c and

the empty list empty.

Where’s empty?

Empty is also understood to be part of every list…

We could go even further…

Intermediate Student gives us the ability to shorten our input even further using the quote. In previous examples, quote was only used to say “interpret the next value literally”. ‘a is the symbol a. Now, the combination ‘( allows us to say “What follows will be a list of items. Quote where applicable.”

Which makes this:

(cons (cons ‘a (cons ‘b empty)) (cons (cons (cons ‘c (cons ‘d empty)) empty) (cons ‘f empty)))

As easy as saying this:

'((a b) ((c d)) f)

Do my old functions still work?

Yep…

(first <list>)

(rest <list>)

(empty? <list>)

(cons? <cons cell>)

(list? <list>)

Next Time…

Chapter 14…Trees