How can I use a user inputted value in my Function in F#

前端 未结 3 1898
梦如初夏
梦如初夏 2020-12-06 21:48

So I\'m trying to make a simple factorial function in F# that uses a value inputted from the user (using the console, I don\'t know if that makes any difference) but I can\'

3条回答
  •  爱一瞬间的悲伤
    2020-12-06 22:46

    F# does no automatic conversions for you, so you'll need to parse the string:

    open System
    
    let rec fact x =
        if x < 1 then 1
        else x * fact (x - 1)
    
    let input = Console.ReadLine()
    Console.WriteLine(fact (Int32.Parse input))
    

    In theory you would need to convert back to string to print it, but it works because there is an overload for Console.WriteLine that takes an integer and does the conversion.

提交回复
热议问题