Since you did not describe the problem you are trying to solve, this answer is based only on the F # code you posted. I agree that the functional version is a bit messy, but I think it can be clearer. I really don't understand the nested for loop in your imperative solution:
for increment_index in {1..4} do current <- current + increment total <- total + current
You are not using increment_index for anything, so you can just multiply increment and current by four and get the same result:
total <- total + 4*current + 10*increment current <- current + 4*increment
Then your imperative decision will be:
let mutable total = 0 let mutable increment = 2 let mutable current = 1 for spiral_layer_index in {1..(dimensions- 1) / 2} do total <- total + 4*current + 10*increment current <- current + 4*increment increment <- increment + 2 total
If you rewrite this to a recursive function, it becomes simple:
let rec loop index (total, current, increment) = if index > (dimensions - 1) / 2 then total else loop (index + 1) ( total + 4*current + 10*increment, current + 4*increment, increment + 2 ) let total = loop 1 (0, 2, 1)
The same could be written using Seq.fold like this (this is even more βfunctionalβ because in functional programming you use recursion only to implement basic functions like fold , which can then be reused):
let total, _, _= {1 .. (dimensions - 1) / 2} |> Seq.fold (fun (total, current, increment) _ -> (total + 4*current + 10*increment, current + 4 * increment, increment + 2)) (0, 1, 2)
NOTE. I am not sure if this really implements what you want. This is simply simplifying your imperative solution, and then rewrite it with a recursive function ...