Title: CS1321: Introduction to Programming
1CS1321Introduction to Programming
- Georgia Institute of Technology
- College of Computing
- Breakout 05
- Inserting into an Ordered List
2Ordered Lists
We want to have TA structure instances stored in
a list in ascending order based on salary.
3Lets start here given a list of TAs that is
already sorted. You want to add one new TA to
that list.
4Insertion
Given the following list
There are basically 3 areas to insert a new value
5Insertion
We can insert an item before the first item in
our list Thats pretty easy
6Insertion
We can insert an item after the last item in our
list This takes a little more work, but is still
pretty easy, conceptually.
7Insertion
Or, we can insert an item somewhere in the middle
of our list This one takes a little thought to
understand
8Tasks
- If our list of sorted TAs is empty, we need to be
able to add our new TA to that empty list - If our list is not empty, does the new TA have a
lower salary than the current TA on the list? - If so, then insert the new one in front of the
old one. - Otherwise, just use recursion to insert the new
TA somewhere in the rest of the list!
9A bit of Abstraction
Lets abstract away that little testing to see if
one TAs salary is less than anothers. Contrac
t paid-less? ta-record ta-record -gt
boolean definition (define (paid-less? TAone
TAtwo) (lt (ta-record-salary TAone)
(ta-record-salary TAtwo)))
10The Insertion Function
(define (insert new-ta ta-list)
- Lets remember what we have to do
- If the list is empty, just cons the new TA on
- Otherwise, find a place where the new
- TA is should go. Insert the TA there.
11The Insertion Function
(define (insert new-ta ta-list) (cond
(empty? ta-list) (cons new-ta empty)
12The Insertion Function
(define (insert new-ta ta-list) (cond
(empty? ta-list) (cons new-ta empty) else
(cond (paid-less? new-ta (first
ta-list))
If the new teaching assistant is paid less than
the existing TA, is this the proper place to
insert the new one?
13The Insertion Function
(define (insert new-ta ta-list) (cond
(empty? ta-list) (cons new-ta empty) else
(cond (paid-less? new-ta (first
ta-list)) (cons new-ta ta-list)
Yes. Just cons the new TA onto the existing
list.
14The Insertion Function
(define (insert new-ta ta-list) (cond
(empty? ta-list) (cons new-ta empty) else
(cond (paid-less? new-ta (first
ta-list)) (cons new-ta ta-list)
Else???
15The Insertion Function
(define (insert new-ta ta-list) (cond
(empty? ta-list) (cons new-ta empty) else
(cond (paid-less? new-ta (first
ta-list)) (cons new-ta ta-list)
Else insert it later
16The Insertion Function
(define (insert new-ta ta-list) (cond
(empty? ta-list) (cons new-ta empty) else
(cond (paid-less? new-ta (first
ta-list)) (cons new-ta ta-list)
else (cons (first ta-list)
(insert new-ta (rest ta-list))))))
17Does this work?
empty
bubba
bob
billy
6.5
7.5
5.5
300
300
300
18Does this work?
empty
bubba
bob
billy
6.5
7.5
5.5
300
300
300
bryan
4.5
300
19Does this work?
empty
bubba
bob
billy
6.5
7.5
5.5
300
300
300
bryan
4.5
300
20Does this work?
empty
bubba
bob
billy
6.5
7.5
5.5
300
300
300
bryan
4.5
300
21Does this work?
empty
bubba
bob
billy
6.5
7.5
5.5
300
300
300
bryan
7.0
300
22Does this work?
empty
bubba
bob
billy
This isnt right (cons first of the list onto
recursive call with rest of the list)
6.5
7.5
5.5
300
300
300
bryan
7.0
300
23Does this work?
empty
bubba
bob
billy
6.5
7.5
5.5
300
300
300
bryan
NO! Keep going! consing as we go
7.0
300
24Does this work?
empty
bubba
bob
billy
6.5
7.5
5.5
300
300
300
bryan
7.0
300
25Does this work?
empty
bubba
bob
billy
6.5
7.5
5.5
300
300
300
bryan
7.0
300
26And to cut a long story short
bubba
bob
billy
6.5
7.5
5.5
300
300
300
empty
bryan
8.0
300
27Our Task
The Insertion Sort. It will take in an unordered
list of TAs, and produce a list of the same TAs,
but with the TAs placed in the list in ascending
order based on TA salary.
28So How Do We Work this?
Lets 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)) ))
29Lets fill in the function based on the template
The easy parts (define (sort myList) (cond
(empty? myList) empty else (first
myList) (sort (rest myList)) ))
30Nows the sticky part
What do we do if our list is not empty?
(else (first myList) (sort
(rest myList))
What do we know?
- We can only deal with the first item directly
- Were writing a function sort which will sort a
list
31What 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 trust the recursive
call to handle the sorting of the rest of the
list
32We find our solution
(define (sort myList) (cond (empty? myList)
empty else (insert (first myList)
(sort (rest myList)))))
Wait a sec ... Where's the rest of it?
33Congratulations!
Youve just written insertion-sort! (We could
also test it with an insertion-sort on a list
that only takes numbers ) More examples read
chapter 12!
34Intermezzo II
35Hooray! Were no longer beginners!
You are no longer beginners. Its time for you
to step up to the next level of scheme
Intermediate
36So whats changed?
Click here for A Full Comparison Between Language
Levels The biggest impact this switch in
language has is when describing lists
37Before and After
38Intermediate Mode Lets Us Abbreviate
No longer do we have to type in (cons a
(cons b (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)
39(list
)
a
b
c
40We 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.
41Which 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)
42(No Transcript)
43Do my old functions still work?
Yes (first ltlistgt) (rest ltlistgt)
(empty? ltlistgt) (cons? ltcons cellgt)
(list? ltlistgt)