3 ms·
"i had to write very similar code (deleting from a linked list) a couple of days ago. i wasn't the easiest code to write, and i needed several iterations before
by betterunix 14y ago
"i had to write very similar code (deleting from a linked list) a couple of days ago. i wasn't the easiest code to write, and i needed several iterations before my tests were ok, but i remember feeling happy that the final result was clear and correct."
How was this not easy code to write? I imagine it would have looked like this, but in your language of choice:
(defun remove-if (lst pred)
(if (null lst)
nil
(if (funcall pred (first lst))
(remove-if (rest lst) pred)
(cons (first lst) (remove-if (rest lst) pred))
)
)
)
This is straightforward and basically follows from the definition of a list: either null (empty) or a pair whose second element is a list. The C version is uglier because of the need to deallocate removed nodes, track pointers, etc., but it still follows from the definition of a list.
- deleted 14y ago[deleted]
- andrewcooke 14y agoeh, what do you want me to say? everyone else here is calling me a smartass and you're telling me i'm an idiot. it was c. i made some mistakes. i fixed them.
- betterunix 14y agoWell I don't mean to say that you are an idiot; I really am curious about what made removing nodes from a list difficult. One of the things that makes linked lists great to work with is how easily nodes can be removed or inserted. Personally, I would be inclined to blame C as a language, rather than you as a programmer. You basically did that anyway: "it was c. i made some mistakes. i fixed them."
- eps 14y ago> everyone else here is calling me a smartass and you're telling me i'm an idiot. This is pure gold :D