F# type mismatch while calling function

此生再无相见时 提交于 2019-12-25 11:57:12

问题


This code

let rec readNLines n list =
 if n = 0 then
  list
 else 
  readNLines(n-1,readInt()::list)

ends with

Type mismatch. Expecting a 'a but given a 'a -> 'a    
The resulting type would be infinite when unifying ''a' and
''a -> 'a' (using built-in F# compiler)

but runs ok when last line is changed to

  readNLines(n-1,(readInt()::list))

or

  readNLines(n-1)(readInt()::list)

Question is: Why? :|


回答1:


Only the last version can work, because readNLines takes two arguments, but

readNLines (n - 1, readInt() :: list)

passes only one argument (which is a tuple consisting of an int and the list).

readNLines (n - 1) (readInt() :: list)

passes them as two separate arguments - the difference here is using the comma (tuple) and space (two arguments).

By the way, that becomes much clearer when you use more whitespace (as I did), because the individual elements are easier to identify.




回答2:


Take a look at these two functions:

> let f1 a b = a + b

val f1 : a:int -> b:int -> int

> let f2 (a, b) = a + b

val f2 : a:int * b:int -> int

As you can see, they have slightly different types. In function f1 you partially apply the arguments (you'll see the term 'curried function' used here), in function f2 you pass in a tuple of arguments in one "go", or you can think of it as only ever having a single argument (an 'uncurried' function).

What you're doing is defining a function f1 style, but later calling it f2 style, which confuses the compiler.



来源:https://stackoverflow.com/questions/32236509/f-type-mismatch-while-calling-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!