Chapter 15: Part 4 equal-simple function
Scheme Function Example
Introduction
- This lecture focuses on an example function in Scheme called
equal-simple. It builds upon the previous lectures for Chapter 15, particularly the concept of thememberfunction.
Review of the member Function
- The
memberfunction takes two parameters: an item and a list. - It checks if the item is an element of the provided list.
- If the item is found within the list, the function returns true; otherwise, it returns false.
Introduction to the equal-simple Function
- The
equal-simplefunction takes two lists as input. - It compares the two lists to determine if they are equal.
- If the lists are equal, the function returns true; otherwise, it returns false.
Scenarios for Comparing Two Lists
Both lists are null lists: In this case, the lists are considered equal and the function should return true.
- Example:
list1is '()' andlist2is '()'.
- Example:
List one is a null list, but list two is not: In this scenario, the lists are not equal, and the function should return false.
- Example:
list1is '()' andlist2is '('a')'.
- Example:
List one is not a null list, but list two is a null list: Similar to scenario 2, the lists are not equal, and the function should return false.
- Example:
list1is '('a' 'b')' andlist2is '()'.
- Example:
Both lists are not null lists: This is the most complex scenario. The function needs to check the elements of both lists to determine equality.
- The function should compare the
car(first element) oflist1with thecaroflist2.- If the
carelements are not equal, then the lists are not equal, and the function should return false. - If the
carelements are equal, the function needs to check the remaining elements (thecdrof the lists) recursively.
- If the
- The function should compare the
Code Implementation of equal-simple
- The function is implemented using a
condstatement with multiple predicates.
Predicate 1
- Checks if
list1is a null list using thenull?function. - If
list1is null:- It then checks if
list2is also a null list.- If
list2is null, it returnstrue. - If
list2is not null, it returnsfalse.
- If
- It then checks if
- This predicate handles scenarios 1 and 2.
Predicate 2
- This predicate is reached only if
list1is not a null list (Predicate 1 is false). - It checks if
list2is a null list. - If
list2is a null list, it returnsfalsebecauselist1is not null, thus handling scenario 3.
Predicate 3
- This predicate is reached only if both
list1andlist2are not null lists. - It checks if the
caroflist1is equal to thecaroflist2. - If they are equal:
- It recursively calls the
equal-simplefunction with thecdroflist1and thecdroflist2.
- It recursively calls the
Else Part
- The
elsepart is reached if thecaroflist1is not equal to thecaroflist2. - In this case, the function immediately returns
falsebecause the lists are not equal.
Tracing Example 1: (equal-simple '(a b c) '(a c b))
- Initial call:
(equal-simple '(a b c) '(a c b))list1andlist2are not null.- Predicate 3 is evaluated:
(car '(a b c)) = (car '(a c b)) => 'a' = 'a' => true - Recursive call:
(equal-simple '(b c) '(c b))
- Recursive call:
(equal-simple '(b c) '(c b))list1andlist2are not null.- Predicate 3 is evaluated:
(car '(b c)) = (car '(c b)) => 'b' = 'c' => false - The
elsepart is executed, returningfalse.
- The result
falseis returned to the initial call, so the final result isfalse.
Tracing Example 2: (equal-simple '(a b) '(a b))
- Initial call:
(equal-simple '(a b) '(a b))list1andlist2are not null.- Predicate 3 is evaluated:
(car '(a b)) = (car '(a b)) => 'a' = 'a' => true - Recursive call:
(equal-simple '(b) '(b))
- Recursive call:
(equal-simple '(b) '(b))list1andlist2are not null.- Predicate 3 is evaluated:
(car '(b)) = (car '(b)) => 'b' = 'b' => true - Recursive call:
(equal-simple '() '())
- Recursive call:
(equal-simple '() '())list1is null, so Predicate 1 is true.list2is also null, so the expression associated with Predicate 1 returnstrue.
- The result
trueis returned up the call stack, leading to a final result oftrue.
Execution in DrRacket: equal-simple and member function
- The video demonstrates running the
equal-simpleandmemberfunctions within the DrRacket environment. - The code for the functions from the slides should be written in DrRacket.
- Call the functions with different list to check if they are working correctly.
Other Scheme Function Examples:
car: Returns the first element of a list.- Example:
(car '(a b c))returns'a'.
- Example:
cdr: Returns the rest of the list, excluding the first element.- Example:
(cdr '(a b c))returns'(b c)'.
- Example:
cons: Adds an element to the beginning of a list.- Example:
(cons 'a '(b c))returns'(a b c)'.
- Example:
Null List Examples
- The video shows several examples of checking if different expressions return true or false when being ran with null.
Using Symbols
- Here an example of assigning a value to a symbol, such as pi:
- Example:
(define pi 3.14) - Then if you call pi, it will return 3.14
- Example:
Upcoming Quiz Information
- There will be a quiz on Friday covering the material discussed in this lecture and the lecture from Monday.