I am experienced in C #, but new to F # and functional programming. Now I am trying to implement a class library in F #. Here is one of the functions: It takes a list of integers <= 9 and changes consecutive 9, for example, 9,9,9,9-9, 10, 11, 12. For example [9; nine; nine; 1; 4; 0; 1; nine; nine; nine; 9] will be changed to [9; 10; eleven; 1; 4; 0; 1; nine; 10; eleven; 12].
C # function is trivial:
void ReleaseCap(List<int> items)
{
for (int i = 1; i < items.Count; i++)
{
var current = items[i];
var previous = items[i - 1];
if (current == 9 && previous >= 9)
{
items[i] = previous + 1;
}
}
}
Now my F # tail is recursive. Instead of quoting the list by index, it recursively moves the item from the original list to the processed list until everything in the original list disappears:
let releaseCap items =
let rec loop processed remaining = //tail recursion
match remaining with
| [] -> processed //if nothing left, the job is done.
| current :: rest when current = 9 -> //if current item =9
match processed with
// previous value >= 9, then append previous+1 to the processed list
| previous :: _ when previous >= 9 -> loop (previous+1 :: processed) rest
//if previous < 9, the current one should be just 9
| _ -> loop (current :: processed) rest
//otherwise, just put the current value to the processed list
| current :: rest -> loop (current :: processed) rest
loop [] items |> List.rev
Although the C # version is trivial and intuitive, the F # code is verbose and not intuitive. Is there any part of the F # code that can be improved to make it more elegant?