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);;