
8 Mar
2006
8 Mar
'06
2:30 p.m.
changeValue x i [xs] = [xs] !! i = x
where x is the value to change to, i is the index of the value to change, and [xs] is the list.
This however, dosen't work in Haskell. How would this be done in Haskell?
Think about what parts of the list you can reuse, how you can define those parts and put them together. You probably want to split the list at the index. 'splitAt' is the function to use for this, as it will give you the resulting prefix and suffix. The head of the suffix is the element you want to replace. So you create a new list by appending the unaltered prefix, the new element and the 'tail' of the suffix. Regards, Jens