F# F中的嵌套if语句#
我有以下代码F# F中的嵌套if语句#,f#,functional-programming,F#,Functional Programming,我有以下代码 let rec function1 element1 element2 = function | [] -> [] | [a;b;c;d;e;f]::t -> if true then if true then [a]::(function1 element1 element2 t) else (function1 element1 element2 t) | h :: t -> (function1 element
let rec function1 element1 element2 = function
| [] -> []
| [a;b;c;d;e;f]::t -> if true then if true then [a]::(function1 element1 element2 t) else (function1 element1 element2 t)
| h :: t -> (function1 element1 element2 t);
,但它不允许我检查语句1和语句2是否正确
我一直在
| [a;b;c;d;e;f]::t -> if true then if true then [a]::(function1 element1 element2 t) else (function1 element1 element2 t)
------------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
stdin(3,31): error FS0001: This expression was expected to have type
'a list
but here has type
unit
我尝试了很多不同的东西,但似乎我并没有尝试一些不应该奏效的东西。请帮助由于您有2个if分支,但只有1个else分支,编译器已将返回类型推断为unit,因为只有1个if表达式返回非unit 你可以用任何一种方法来解决这个问题 1:删除嵌套-如果可以&&条件,则无需删除嵌套
2:添加额外的else分支(在您的情况下,它将返回(function1element1element2t))因为您有2个if分支,但只有1个else分支,编译器将返回类型推断为unit,因为只有1个if表达式返回非unit 你可以用任何一种方法来解决这个问题 1:删除嵌套-如果可以&&条件,则无需删除嵌套
2:添加额外的else分支(在您的情况下将返回(function1element1element2t))您的语句如下所示:
if true
then
if true
then something
else somethingElse
// missing else
如果整体表达式的类型应该是unit
,If语句可能会省略else
分支。因此,当您省略else
时,您可以考虑由编译器添加else()
另一方面,您正在定义
match
语句应返回一个列表。您的语句如下所示:
if true
then
if true
then something
else somethingElse
// missing else
如果整体表达式的类型应该是unit
,If语句可能会省略else
分支。因此,当您省略else
时,您可以考虑由编译器添加else()
另一方面,您正在定义match
语句应该返回一个列表