3 ms·
Zippers clicked for me when I realized that they are a way to traverse immutable data structures by allocating like crazy. For example, you can have a pair of l
by cousin_it 11y ago
Zippers clicked for me when I realized that they are a way to traverse immutable data structures by allocating like crazy. For example, you can have a pair of lists, "left" and "right". To "move left", you create a new pair of lists: the left list minus one element, and the right list plus one element. The same idea also works for traversing other data structures, like trees.
- tel 11y agoAnother way of looking at it that's nice is that they "rewire the pointers". If a LL looks a bit like this [1] -> [2] -> [3] -> [4] -> [] where the [n] location actually represents a data pointer then stepping into the zipper rearranges some of these... [1] <- [2] -> [3] -> [4] -> [] [1] <- [2] <- [3] -> [4] -> [] To be a bit more explicit we should talk about where "we" are * -> [1] -> [2] -> [3] -> [4] -> [] [1] <- * -> [2] -> [3] -> [4] -> [] [1] <- [2] <- * -> [3] -> [4] -> [] etc etc.
- cousin_it 11y agoTo explain stuff to imperative programmers, you need to adopt the operational perspective. Spell out what's actually in memory at any given time, what gets executed after what, where the allocations happen, what's left for the GC to pick up, etc. If you tell them that zippers "rewire the pointers", they will get rightly confused, because rewiring existing pointers is impossible in pure code. Zippers allocate new pointers instead. That's what you should say.