调用函数时F#类型不匹配 [英] F# type mismatch while calling function

查看:68
本文介绍了调用函数时F#类型不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码

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

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)

但是在最后一行更改为

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

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

问题是:为什么? :|

Question is: Why? :|

推荐答案

仅最后一个版本可以使用,因为readNLines带有两个参数,但是

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

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

仅传递一个参数(这是一个由intlist组成的元组).

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.

这篇关于调用函数时F#类型不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆