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#工作的第一天。我在Haskell呆了一段时间,试图学习F#如何使用一些现有的.net代码。为什么这个代码对我生气?什么是等价物:t?如何将类型注释添加到main type Color = Red | Green [<EntryPoint>] let main = let a = Red if a = Red then printfn "hi!" else printfn "no!" 类型颜色=红色|绿色 [] 让main

在F#工作的第一天。我在Haskell呆了一段时间,试图学习F#如何使用一些现有的.net代码。为什么这个代码对我生气?什么是等价物:t?如何将类型注释添加到main

type Color = Red | Green
[<EntryPoint>]
let main = 
    let a = Red
    if a = Red then
       printfn "hi!"
     else
       printfn "no!"
类型颜色=红色|绿色
[]
让main=
让a=红色
如果a=红色,则
“嗨
其他的
printfn“不

具有
[]
属性(例如
main
)的函数签名应为
string[]->int
在您的版本中,它缺少
string[]
参数和类型为
int
的返回值

您可以通过添加以下内容来解决此问题:

type Color = Red | Green
[<EntryPoint>]
let main argv = //argv added here is inferred to be string[]
    let a = Red
    if a = Red then
       printfn "hi!"
     else
       printfn "no!"
    0 //Return 0, all OK
类型颜色=红色|绿色
[]
让main argv=//此处添加的argv被推断为字符串[]
让a=红色
如果a=红色,则
“嗨
其他的
printfn“不
0//返回0,一切正常

如果末尾没有
0
返回
int
,则返回
unit
(打印的结果fn)。

带有
[]
属性(例如
main
)的函数签名应该是
字符串[]->int
,在您的版本中,它缺少
字符串[]
参数和类型为
int

您可以通过添加以下内容来解决此问题:

type Color = Red | Green
[<EntryPoint>]
let main argv = //argv added here is inferred to be string[]
    let a = Red
    if a = Red then
       printfn "hi!"
     else
       printfn "no!"
    0 //Return 0, all OK
类型颜色=红色|绿色
[]
让main argv=//此处添加的argv被推断为字符串[]
让a=红色
如果a=红色,则
“嗨
其他的
printfn“不
0//返回0,一切正常

如果最后没有
0
来返回
int
,那么你就是在返回
unit
printfn
的结果)。

在这样一个简单的应用程序中,你不必使用
main
:@FoggyFinder-很好的一点,通常最好开始在FSI中胡闹,在VS/VSCode/Ionide/etc中使用脚本。在这样一个简单的应用程序中,您不一定要使用
main
:@FoggyFinder-好的方面,通常最好开始在FSI中使用VS/VSCode/Ionide/etc中的脚本。我试图在interactive中运行它,我必须使用eg[|“1”;“2”;“3”|]。管道在这里做什么?这是一个严格的列表吗?请在FSI:)中尝试。有了
让a=[|“a”;[124;]
=
val a:string[][a”[124;]
没有:
val a:string[][a”][a”][/code>=
val l:string list=[“l”][/code>。哦,还有@ProbablyAStupidQuestion-请随时提出新问题,这些评论并不是为新问题而设计的——而且F#问题的回答时间非常快。我试图在interactive中运行它,我必须使用eg[|“1”;“2”;“3”|]。管道在这里做什么?这是一个严格的列表吗?请在FSI:)中尝试。有了
让a=[|“a”;[124;]
=
val a:string[][a”[124;]
没有:
val a:string[][a”][a”][/code>=
val l:string list=[“l”][/code>。哦,还有@ProbablyAStupidQuestion-请随时提出新问题,这些评论并不是为新问题而设计的——而F#问题的回答时间非常快。