我有以下代码:
let rec nextUnusedInteger (r:Random) rangeMax used =
let index = r.Next(0, rangeMax)
match used |> List.tryFind (fun i -> i = index) with
| None -> index
| Some i -> nextUnusedInteger r rangeMax used
let rec buildRandomIntegerList (rand:Random) rangeMax reserved count acc =
match count with
| 0 -> acc
| _ ->
let randomInteger = nextUnusedInteger rand rangeMax reserved
let newCount = count - 1
buildRandomIntegerList rand rangemax randomInteger::reserved newCount randomInteger::acc
我收到了以下编译错误:
Type mismatch. Expecting a 'a but given a int -> 'a list -> 'a list
The resulting type would be infinite when unifying ''a'
and 'int -> 'a list -> 'a list'
This value is not a function and cannot be applied
问题是内联缺点。 randomInteger :: reserved和randomInteger :: acc。如果我明确地为那些创造价值,它就有效。以下是修订后的代码:
let rec nextUnusedInteger rangeMax used =
let rand = new Random()
let index = rand.Next(0, rangeMax)
match used |> List.tryFind (fun i -> i = index) with
| None -> index
| Some i -> nextUnusedInteger rangeMax used
let rec buildRandomIntegerList rangeMax reserved count acc =
match count with
| 0 -> acc
| _ ->
let randomInteger = nextUnusedInteger rangeMax reserved
let newCount = count - 1
let newReserved = randomInteger::reserved
let newAcc= randomInteger::acc
buildRandomIntegerList rangeMax newReserved newCount newAcc
问题是为什么?我一直看到使用内联Cons的例子。为什么原始代码不起作用?
答案 0 :(得分:3)
您可以使用内联cons,但如果结果作为函数参数传递,则需要括号:
buildRandomIntegerList rand rangemax (randomInteger::reserved) newCount (randomInteger::acc)
否则编译器会创建一个部分应用函数并尝试将某些内容连接到一个函数,这没有任何意义,这就是错误消息告诉你的内容。
同样适用于其他运营商,功能应用优先于它们。