这是一个高度简化的金融算法版本。实际条件和数据处理中有更多逻辑被 "myFunc" 表示,但在这个版本中并不打算使其有意义。
我的问题是:我该如何理解在此块中使用
我想我不明白为什么
我希望这个问题和例子能引出一些关于如何思考(嵌套)范围的见解。(当然,我也很高兴听到关于设计的其他评论,但请记住,这段代码被极度剥离了很多分析。)(我可能在剥离它时使算法变得不合逻辑,但我试图专注于范围的问题。)
我的问题是:我该如何理解在此块中使用
firstMultiple
是允许且合理的。为什么它应该起作用?表达式直接从标记为 A
的行映射到使用其值的标记为 C3
的行。但是,在与 C3
大致相同的“缩进”级别的 C1
和 C2
中,在这里似乎将 firstMultiple
分配为可变的-??。我想我不明白为什么
C3
使用 firstMultiple
,而 C1
可以看似地重写它。(调试器和运行结果表明这是可以的)。我希望这个问题和例子能引出一些关于如何思考(嵌套)范围的见解。(当然,我也很高兴听到关于设计的其他评论,但请记住,这段代码被极度剥离了很多分析。)(我可能在剥离它时使算法变得不合逻辑,但我试图专注于范围的问题。)
let rec ListBuilder factor firstMultiple useFirstMultiple inputList outputList = // "A"
match inputList with
| [] -> []
| h::[] -> List.rev (inputList.Head :: outputList)
| _ ->
let nextInput = inputList.Head
let newOutputList, remInputList, firstMultiple = // "B"
match outputList with
| [] -> //first pass, capture firstMultiple now
let firstMultiple = nextInput * factor // "C1"
[nextInput], inputList.Tail, firstMultiple // "C2"
| _ ->
let lastOutput = outputList.Head
let multiple =
if useFirstMultiple then firstMultiple // "C3"
else lastOutput * factor
let newOutputList =
if (myfunc multiple nextInput: bool) then
nextInput :: outputList
else
outputList
let remInputList =
if not (myfunc multiple nextInput: bool) then
inputList.Tail
else
inputList
newOutputList, remInputList, firstMultiple
ListBuilder factor firstMultiple useFirstMultiple remInputList newOutputList