gpt4 book ai didi

function - F#:如何使用参数Byref Int调用函数

转载 作者:行者123 更新时间:2023-12-02 06:52:35 25 4
gpt4 key购买 nike

我有这个代码:

let sumfunc(n: int byref) =
let mutable s = 0
while n >= 1 do
s <- n + (n-1)
n <- n-1
printfn "%i" s

sumfunc 6

我得到错误:
(8,10): error FS0001: This expression was expected to have type
'byref<int>'
but here has type
'int'

因此,我可以说出问题所在,但我只是不知道如何解决。我想我需要以某种方式将数字6指定为 byref<int>。我只是不知道如何。我的主要目的是使 n或函数参数可变,以便我可以在函数内更改和使用其值。

最佳答案

这是一个坏主意:

let  mutable n = 7
let sumfunc2 (n: int byref) =
let mutable s = 0
while n >= 1 do
s <- n + (n - 1)
n <- n-1
printfn "%i" s

sumfunc2 (&n)

完全同意munn的评论,这是另一种内爆方式:
let sumfunc3 (n: int)  =
let mutable s = n
while s >= 1 do
let n = s + (s - 1)
s <- (s-1)
printfn "%i" n

sumfunc3 7

关于function - F#:如何使用参数Byref Int调用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39698430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com