问题描述
我正在编写一个F#函数,该函数将数字分解为素数.
I'm writing an F# function that factorises a number into prime factors.
let factors primes i =
let mutable j = i
for p in primes do
while (j>1) && (j%p=0) do
j <- j/p
printfn "prime: %i" p
它适用于i
的int
值,但不适用于int64
值.参数primes
是一组int值.
It works for int
values of i
, but not int64
values. The parameter primes
is a set of int values.
我理解为什么会这样-类型推断是假设函数仅使用int
参数-但我想将参数类型明确指定为int64
.
I understand why this is the case - type inference is assuming that the function only takes int
parameters - but I want to explicitly specify the parameter type as int64
.
是否可以编写此函数以便它对int
和int64
均适用?
Is it possible to write this function so that it will work for both int
and int64
?
推荐答案
如果只想使用int64
值,只需将1
和0
分别替换为1L
和0L
. jpalmer的答案涵盖了一般情况.
If you want to work only on int64
values, just replace 1
and 0
with 1L
and 0L
respectively. jpalmer's answer covers the generic case.
这篇关于在F#中明确指定参数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!