Processing Lists Part V PowerPoint PPT Presentation

presentation player overlay
1 / 27
About This Presentation
Transcript and Presenter's Notes

Title: Processing Lists Part V


1
Processing ListsPart V
2
Outline
  • Prerequisites
  • Normal list processing
  • Case I second list constant
  • Case II lists in lock-step
  • Case III list indexing
  • Case IV independent lists
  • Objectives
  • List equality

3
Background
  • We know how to process lists several ways
  • What happens when we want to determine whether
    two lists are identical?
  • This is simple in principle, but we have to cover
    a number of special cases

4
Program Requirements
  • Objective
  • Given two lists
  • Return true if each item on each list is
    identical
  • Otherwise, return false

5
Program Design
  • list? list-1 list-2 -gt boolean
  • determine whether two lists are
  • identical

List 1
List 2
fred
fred
Case 1
jean
jean
sally
sue
juan
juan
carl
carl
lien
6
Program Design
  • list? list-1 list-2 -gt boolean
  • determine whether two lists are
  • identical

List 1
List 2
fred
fred
Case 1
jean
jean
sally
sue
juan
juan
carl
carl
lien
7
Program Design
  • list? list-1 list-2 -gt boolean
  • determine whether two lists are
  • identical

List 1
List 2
fred
fred
Case 1
jean
jean
sally
sue
juan
juan
carl
carl
lien
8
Program Design
  • list? list-1 list-2 -gt boolean
  • determine whether two lists are
  • identical

List 1
List 2
fred
fred
Case 2
jean
jean
sally
sally
juan
juan
carl
carl
lien
9
Program Design
  • list? list-1 list-2 -gt boolean
  • determine whether two lists are
  • identical

List 1
List 2
fred
fred
Case 2
jean
jean
sally
sally
juan
juan
carl
carl
lien
10
Program Design
  • list? list-1 list-2 -gt boolean
  • determine whether two lists are
  • identical

List 1
List 2
fred
fred
Case 2
jean
jean
sally
sally
juan
juan
carl
carl
lien
11
Program Design
  • list? list-1 list-2 -gt boolean
  • determine whether two lists are
  • identical

List 1
List 2
fred
fred
Case 2
jean
jean
sally
sally
juan
juan
carl
carl
lien
12
Program Design
  • list? list-1 list-2 -gt boolean
  • determine whether two lists are
  • identical

List 1
List 2
fred
fred
Case 2
jean
jean
sally
sally
juan
juan
carl
carl
lien
13
Program Design
  • list? list-1 list-2 -gt boolean
  • determine whether two lists are
  • identical

List 1
List 2
fred
fred
Case 2
jean
jean
sally
sally
juan
juan
carl
carl
lien
14
Program Design
  • list? list-1 list-2 -gt boolean
  • determine whether two lists are
  • identical

List 1
List 2
fred
fred
Case 3
jean
jean
sally
sally
juan
juan
carl
carl
15
Program Design
  • list? list-1 list-2 -gt boolean
  • determine whether two lists are
  • identical

List 1
List 2
fred
fred
Case 3
jean
jean
sally
sally
juan
juan
carl
carl
16
Program Design
  • list? list-1 list-2 -gt boolean
  • determine whether two lists are
  • identical

List 1
List 2
fred
fred
Case 3
jean
jean
sally
sally
juan
juan
carl
carl
17
Program Design
  • list? list-1 list-2 -gt boolean
  • determine whether two lists are
  • identical

List 1
List 2
fred
fred
Case 3
jean
jean
sally
sally
juan
juan
carl
carl
18
Program Design
  • list? list-1 list-2 -gt boolean
  • determine whether two lists are
  • identical

List 1
List 2
fred
fred
Case 3
jean
jean
sally
sally
juan
juan
carl
carl
19
Program Design
  • list? list-1 list-2 -gt boolean
  • determine whether two lists are
  • identical

List 1
List 2
fred
fred
Case 3
jean
jean
sally
sally
juan
juan
carl
carl
20
Program Design
  • Cases

list-1 empty
-gt (empty? list-2)
list-2 empty
-gt return false
both list-1 and list-2 are lists
-gt return (and ( (first list-1)
(first list-2))) (list?
(rest list-1) (rest
list-2)))
21
Short-Circuit Evaluation
Evaluating (and ( (first list-1)(first
list-2))) (list? (rest list-1)
(rest list-2)))
22
Short-Circuit Evaluation
  • When we evaluate (and expr-1 expr-2),
  • as soon as either expression evaluates to false,
    the total result can no longer be true.
  • In the particular case of evaluating
  • (and ( (first list-1)(first list-2)))
  • (list? (rest list-1)
  • (rest list-2)))
  • expr-1 looks at the head of each list
  • As long as this result is true, the recursive
    evaluation of (list? ) continues
  • As soon as the items are not equal, the recursive
    part is not evaluated
  • Recursion stops as you would expect as soon as a
    mis-match is detected.

23
(No Transcript)
24
Code Used
  • list? list-1 list-2 -gt boolean
  • determine whether two lists are
  • identical
  • (define (list? list-1 list-2)
  • (cond (empty? list-1) (empty? list-2)
  • (empty? list-2) false
  • (and (cons? list-1) (cons? list-2))
  • (and ( (first list-1)
  • (first list-2))
  • (list? (rest list-1)
  • (rest list-2)))
  • else "error - ill-formed problem"))

25
Questions?
26
Summary
  • You should now know
  • List equality

27
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com