Warning This class is being recorded. At least I think it is.
Approximate overview
Academic
Cultural
Peer
Wellness
Misc
You can ask questions about anything related to the class! Self gov says that you should ask those questions.
What should I do for an MP redo?
Fix the things the graders noted made it below an E.
Add a
CHANGES.md
file that tells the graders what you changed.
Will we get MP2 and MP3 back before MP4 is due?
I hope so. It’s my fault.
(car lst)
- get the first element in the list (some people
call this head
(cdr lst)
- get all but the first element in the list (some
people call this tail
(cons x xs)
- makes a new list by adding x
to the front of xs
.null
- the empty list(empty? lst)
- is the list empty(make-list n val)
- makes a list of n
copies of val
.list-ref
index-of
(length lst)
- determines how many values are in the list.(map fun lst)
- apply fun to each element of the list.(define map
(lambda (fun lst)
(if (null? lst)
null
(cons (fun (car lst))
(map fun (cdr lst))))))
Sam doesn’t like to include too many operations because (a) it limits our implementatoin and (b) we sometimes forget about the cost.
What is LIA
.
cons
: Build a new two-memory-cell element, each contains a
reference, the first to the value the second to the rest.car
: Follow the first referece.cdr
: Follow the next reference.null
: Designated memory location (often 0).empty?
: Compare a memory location to null.cons
: Constant timecar
: Constant timecdr
: Constant timeempty?
: Constant timelength
: Linear time, depends on the length of the list.If we had laid this out as an array, cons
would likely require us to
build a new array.
interface OurList<T>
)
OurList()
- creates an empty listvoid add(int i, T val)
- Adds a value at a particular index
[Obvious implementation iterates through the list]
i > length()
, throw an exceptioni == length()
, add to the end of the listvoid addToFront(T val)
- Adds a value at the front of the list.void addToEnd(T val)
- Adds a value to the end of the list.void append(OurList other)
- add another list to the end of thisvoid removeAt(index i)
- remove the element at index i
(some folks make thist return the removed value)
void remove(T val)
- remove a value from the list
removeAll
and perhaps even removeLast
.removeAll
, we’re fine.remove
, might consider throwing an exceptionint length()
- find the number of elements in the listisFull()
- determines whether the list is fullOutList(int n)
- create an empty list that holds
up to n
valueSide Note: Linked structures are a way to implement stacks and queues, but they don’t have to be “linked lists”.
Worry: Many of these operations take an index, which suggests that they are going to be slow in practice. Are there other ways to think about manipulating a list?
Somewhat bad mechanism: Have a sense of a current location/value in the list.
T current()
- Get the current element in the list.void advance()
- Move to the next element in the list.void remove()
- Remove the current elementvoid addBefore(T)
- add something before/after the current valueIf we’re using a well-designed linked structure, all of these are probably constant time operations.
Why did Sam call this “somewhat bad”? Sam read a paper called “List with current considered harmful.”
Sam thinks of those separate objects as Cursors
.
getCursorAtFront()
method.current
, advance
, addBefore
, addAfter
,
replace
, remove
.Philosophy: “lists are homogeneous, mutable, linear collections that provide cursors.”
removeAt(int i)
and setAt(int i)
, they
will use them without considering the cost.