4 ms·
> But why are we counting how many times we "touch" the list? Well, right, exactly. Why did you ask for that? Probably there's a reason. If I understand cor
by dllthomas 6y ago
> But why are we counting how many times we "touch" the list?
Well, right, exactly. Why did you ask for that? Probably there's a reason.
If I understand correctly, you are really asking why this needs to apply to all operations, and I think laziness is probably the answer. If I've asked for a linear list because I want to somewhere change it inline, and I ask for the length and "then" do something that changes that length, the value the length function returns would seem to depend on when (at run time) the change is forced vs the question (and they could possibly be overlapping, if you have sent the length to another thread!). That's not what we want.
Requiring you to pass back a "new" list forces the ordering - either my modification is of the list before I took the length, or the list after. In terms of sequencing, that's exactly what we want.
I do agree that it is a bit disappointing that the types don't tell us that length returns list unchanged.