Consider the following definition of the function subst:
1. var subst = function (n,o,t) { 2. if (fp.isNull(t)) { 3. return t; 4. } else if (fp.isList(fp.hd(t))) { 5. return fp.cons(subst(n,o,fp.hd(t)),subst(n,o,fp.tl(t))); 6. } else if (fp.isEq(o,fp.hd(t))) { 7. return fp.cons(n,subst(n,o,fp.tl(t))); 8. } else { 9. return fp.cons(fp.hd(t),subst(n,o,fp.tl(t))); 10. } 11. };
Suppose that you want a function that inserts its first argument before each occurrence of its second argument in its third argument, where that third argument must be a list. Hence a call with the arguments 4, 5, and [1,2,3,[4,5],5,6] would return [1,2,3,[4,4,5],4,5,6]. This could be accomplished by making a small change in one line of the subst function above and leaving the rest of the function unchanged. Which line should you change?
Make sure you understand what the new function is supposed to compute and how it differs from the given subst function.
Make sure that the new function considers all possible cases in the right order.
Remember to look for a solution with a single code change.