News for hackers. Stuff that matters.
Re:Array Iteration Interview Problem by aidenn0 on Wednesday August 01, 2012 @08:43PM (#4326587)
In Common Lisp:

    (defun increment (sequence val n)
      (substitute (1+ val) val sequence
              :from-end (< n 0)
              :count (if (= n 0) nil (abs n)))
Re:Array Iteration Interview Problem by damoe on Wednesday August 01, 2012 @09:38PM (#4326755)
As usual, lisp from the win! But you'd want to use nsubstitute for the in place manipulation.
Re:Array Iteration Interview Problem by spicyj on Wednesday August 01, 2012 @08:53PM (#4326616)
I don't know Common Lisp, but it doesn't look to me like that works. Can you explain how it works?
Re:Array Iteration Interview Problem by nandemo on Wednesday August 01, 2012 @09:42PM (#4326764)
I 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.

Re:Array Iteration Interview Problem by walrus on Wednesday August 01, 2012 @09:35PM (#4326747)
It works. Common Lisp just happens to have a function built in (substitute) that does pretty much what the problem asks for.