在F#中明确指定参数类型 [英] Explicitly specifying parameter types in F#

查看:73
本文介绍了在F#中明确指定参数类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个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

它适用于iint值,但不适用于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.

是否可以编写此函数以便它对intint64均适用?

Is it possible to write this function so that it will work for both int and int64?

推荐答案

如果只想使用int64值,只需将10分别替换为1L0L. 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#中明确指定参数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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