F# For loop negative count down - Type int does not support the operator ..-
f#
Solution
Have changed the function to just count up and it works, but I'm interested to know why this failed.
Your example fails because `..` and `-` are two different operators, the compiler needs separation between those. Instead of wrapping `-1` by parentheses, you could add spaces:
let fact x =
let mutable product = 1
for i in x .. -1 .. 1 do
product <- product * i
product
Is there a better way to count down?
The less well-known `for .. downto .. do` construct is more appropriate to use here.
let fact x =
let mutable product = 1
for i = x downto 1 do
product <- product * i
product
Problem
Am trying to count down from 6 to 1 in a non-recursive factorial function and getting a compiler error: ``` let fact x = let mutable product = 1 for i in x..-1..1 do product <- product * i product // Error on the 6 - type 'int' does not support the operator '..-' let answer = fact 6 printfn "%i" answer ``` I got this idea from near the bottom here Have changed the function to just count up and it works, but I'm interested to know why this failed. Is there a better way to count down? Using VS2012 update 3