4 ms·
I don't know Common Lisp, but it doesn't look to me like that works. Can you explain how it works?
by spicyj 14y ago
I don't know Common Lisp, but it doesn't look to me like that works. Can you explain how it works?
- walrus 14y agoIt works. Common Lisp just happens to have a function built in (substitute) that does pretty much what the problem asks for.
- nandemo 14y agoI don't know Common Lisp either but if you know any Lisp it's quite readable. I'll guess an explanation (without looking at any docs) and let the real lispers correct me. substitute traverses the list sequence replace any element equal to val with val + 1. I don't know if sequence does this in-place but that doesn't necessarily affect the correctness, since this wasn't mentioned in the spec. If used as above then all elements equal to val would be replaced. But there are 2 additional (optional?), named parameters. from-end indicates whether to traverse the list in reverse order. It will be true if and only if n is less than 0. count indicates how many replacements should be made. If n is 0, then this will be nil, which indicates "replace all". Otherwise it will be the absolute value of n. It's likely that the interviewer would ask you to implement substitute, though.