Question: Write a 3-argument function replace which takes a list ls, an integer n and an value x as arguments, and returns a list where the
Write a 3-argument function replace which takes a list ls, an integer n and an value x as arguments, and returns a list where the element in the n th position has been replaced by x. The other elements in the list should stay the same as before. The positions start with 0. If n is not a valid position in ls, then ls itself should be returned.This needs to be done in standard ML.
sample test cases:
- replace; val it = fn: a list * int * a -> a list -
replace(nil, 2, 3); val it = []: int list
- replace([1,2,4], 2, 3); val it = [1, 2, 3]: int list
- replace([1,2,4], 5, 0); val it = [1, 2, 4]: int list
- replace([2.3, 5.0, 2.001, 1.11], 1, 50.0); val it = [2.3, 50.0, 2.001, 1.11]: real list
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
