gpt4 book ai didi

f# - Seq{/* yielding sth */} : Error with mutable variables

转载 作者:行者123 更新时间:2023-12-05 00:36:29 24 4
gpt4 key购买 nike

我正在尝试学习 F#(我是 C# 开发人员),并在尝试编译以下代码时遇到了我的第一个问题:


let decompose n =
seq{
let mutable c = n
let mutable i = 2L
if c%2L=0L then
c <- c/2L
yield 2L
if (c=1L) then yield 1L
else
while c<>1L do
if c % i=0L then
c<-c/i
yield i
else i <- i+2L
}

我试图谷歌编译错误(见下文),但没有成功(可能是因为它是法语):

La variable mutable 'c' est utilisée de manière incorrecte. Impossible de capturer les variables mutables à l'aide de fermetures. Supprimez cette utilisation de la mutation ou utilisez une cellule de référence mutable allouée par tas via 'ref' et '!'.

有人可以帮我解决这个问题吗?或者至少给我错误的英文版本?

谢谢!

最佳答案

好的,伙计们...... StackOverflow 设计得很好:我在下面的帖子中找到了我的答案(在“相关”列中找到):

The mutable variable 'i' is used in an invalid way.?

我不会为将通过谷歌搜索此错误的法国人删除我的问题!

顺便说一句,这是工作代码:

let decompose n = 
seq{
let c = ref 0L
c := n
let i = ref 3L
if !c%2L=0L then
c := !c / 2L
yield 2L
if (!c=1L) then yield 1L
else
while !c<>1L do
if !c % !i=0L then
c:= !c / !i
yield !i
else i := !i+2L
}

// returns [|3;41|]
let dec = decompose 123L |> Seq.ToArray

关于f# - Seq{/* yielding sth */} : Error with mutable variables,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7829948/

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