Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
F# 使用标准输入初始化函数_F# - Fatal编程技术网

F# 使用标准输入初始化函数

F# 使用标准输入初始化函数,f#,F#,我正面临一个让我迷失方向的问题,因为这应该是一个相当基本的过程。我正在学习如何使用F#构建简单的控制台应用程序,但我不知道如何将整数作为标准输入传递给程序。请考虑: open System let square x = x * x [<EntryPoint>] let main (argv :string[]) = printfn "The function will take an input and square it" printfn "%d squar

我正面临一个让我迷失方向的问题,因为这应该是一个相当基本的过程。我正在学习如何使用F#构建简单的控制台应用程序,但我不知道如何将整数作为标准输入传递给程序。请考虑:

open System

let square x = x * x

[<EntryPoint>]
let main (argv :string[]) = 
    printfn "The function will take an input and square it" 
    printfn "%d squared is %d" 12 (square 12)
Console.ReadLine() |> ignore
0
我尝试调整
let x=Console.ReadLine()
以获得整数,但没有明显的结果。我查阅了,主要考虑字符串输入。恐怕我遗漏了一些重要的东西,无法正确理解F#的基本概念。从这个意义上讲,任何建议都将不胜感激。

ReadLine()
将返回一个字符串。正如@JohnPalmer所提到的,它需要被解析为一个int。下面的程序应该给你一个基本的想法:

let rec input () =
    printf "Input: "
    match Core.int.TryParse (stdin.ReadLine ()) with
    | true, i -> i
    | _ ->
        printfn "Invalid input, try again"
        input ()

let square x = x * x

[<EntryPoint>]
let main _ =
    printfn "The function will take an input and square it"
    let i = input ()
    printfn "%d squared is %d" i (square i)
    0
让rec输入()=
printf“输入:
将Core.int.TryParse(stdin.ReadLine())与
|是的,我->我
| _ ->
printfn“输入无效,请重试”
输入()
设平方x=x*x
[]
让主=
printfn“函数将接受输入并将其平方”
设i=输入()
printfn“%d平方等于%d”i(平方i)
0

int.Parse()
是你的朋友谢谢@JohnPalmer,我会做一些练习。非常感谢这个简单的例子。
let rec input () =
    printf "Input: "
    match Core.int.TryParse (stdin.ReadLine ()) with
    | true, i -> i
    | _ ->
        printfn "Invalid input, try again"
        input ()

let square x = x * x

[<EntryPoint>]
let main _ =
    printfn "The function will take an input and square it"
    let i = input ()
    printfn "%d squared is %d" i (square i)
    0