I am learning the ropes of F# through Project Euler, and ran into the following issue a few times. I write a function, run it in the F# interactive window, and the program hangs there. I suspect the function is failing, but I am not getting any significant error message which would help me figure out what is going wrong. Is there any way to debug a program running in F# interactive?
As an illustration, here is an example, from Problem 12. FindFirstTriangle(0,0,100) runs fine, but when the divisor is around 150, things get stuck.
Note: this is not about what is wrong about this code, but about how to figure out where things go wrong!
let NumberOfDivisors n =
[1 .. n] |> List.filter (fun i -> n % i = 0) |> List.length;;
let HasMoreThanDDivisors n d =
if NumberOfDivisors n >= d then
true
else
false
let rec FindFirstTriangle (index, number, divisors) =
if HasMoreThanDDivisors number divisors then
number
else
let nextIndex = index + 1
let nextNumber = number + index
FindFirstTriangle (nextIndex, nextNumber, divisors);;
If you have e.g. ‘windows task manager’ running you’ll see that your CPU is running flat out while the thing is hung. You’ve just created too much work; you need a more efficient algorithm. Press the ‘interrupt’ key in F# interactive (Ctrl-. in the FSI window in Visual Studio) to stop processing.
If the big-oh isn’t clear, you might consider adding some prints to show how much work is being done. E.g.
and then run FindFirstTriangle with larger and larger numbers to get a sense of what’s happening.