







Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!








Lists 14-
IntLists are defined recursively:
An IntList is either
head 8 tail
head 11 tail
head- tail
IntList IntList IntList IntList
empty
// list node // list node // list node // empty list
Lists 14-
Box-and-pointer notation
We represent list nodes and empty lists using a shorthand known as box-and-pointer notation.
head 8 tail
head 11 tail
head- tail
IntList IntList IntList IntList
empty
8 11 -2 Ø
// list node // list node // list node // empty list
Lists 14-
Why are lists useful?
91 0 -22 ... (^38) Ø
list
21 91 0 ... (^38) Ø
list
21
list
Lists 14-
isEmpty method
public static boolean isEmpty (IntList L) Returns true if L is an empty integer list and false if L is an integer list node.
list list list
Lists 14-
head method
public static int head (IntList L) Returns the integer that is the head component of the integer list node L. Signals an exception if L is empty.
list list list
Lists 14-
tail method
public static IntList tail (IntList L) Returns the integer list that is the tail component of the integer list node L. Signals an exception if L is empty.
list list list
Lists 14-
heads and tails
list list list
Lists 14-
Sharing list nodes
list4^4
IntList list4 = IntList.prepend(4, IntList.prepend(3, IntList.prepend(2, IntList.prepend(1, IntList.empty()))));
IntList list5 = IntList.prepend(7, IntList.prepend(4, IntList.tail(IntList.tail(list4))));
list5 7 4 list6 8
IntList list6 = IntList.prepend(8, IntList.tail(list5));
Lists 14-
Recursive list methods
Lists 14-
Recursive list methods
Lists 14-
Recursive list methods
Lists 14-
Squaring numbers
o Method mapSquares(IntList L) returns a list, such that each element in the list is the “square” of the corresponding element in “L”. o For example, mapSquares(L)
Lists 14-
mapSquares(IntList L);
// Returns a new list whose elements are the sqaures of // the corresponding elements in the given list public static IntList mapSquares(IntList L) {
if ( ) { // base case return } else { // recursive case return
Lists 14-
mapSquares(IntList L);
// Returns a new list whose elements are the sqaures of // the corresponding elements in the given list public static IntList mapSquares(IntList L) {
if ( InList.isEmpty(L) ) { // base case return empty(); } else { // recursive case return IntList.prepend(IntList.head(L)*IntList.head(L), mapSquares(IntList.tail(L))); } }
Lists 14-
Filter methods
Afilter method passes some elements through and holds other elements back.
More formally: Afilter method transforms one list to another by keeping only those elements satisfying a given predicate.
L1 L
filter
Lists 14-
filterEven(IntList L);
// Returns sublist containing elements of L that are even public static IntList filterEven(IntList L) {
if ( IntList.isEmpty(L) ) { // base case return IntList.empty(); } else if ( (IntList.head(L) % 2) == 0 ) { // recursive case return IntList.preprend(IntList.head(L), filterEven(IntList.tail(L))); } else { return filterEven(IntList.tail(L)); } }