这个问题在这里已经有了答案:
Shadowing and Nested function
(2 个回答)
5年前关闭。
这是金融算法的高度骨架化版本。这里用“myFunc”表示的实际条件和数据处理有更多的逻辑——这在这个版本中并没有真正的意义。
我的问题是:我怎么理解为什么使用 firstMultiple
在这个 block 是允许和明智的。为什么它应该起作用?
表达式直接从标记为 A
的行映射。到标记为 C3
的行在哪里使用它的值。但后来在 C1
和 C2
-- 与 C3
的“缩进”级别(大致)相同--这里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
最佳答案
因为这个例子基本上是一个循环
let rec ListBuilder
...
ListBuilder
循环命中
C3
outputlist
中的每一项然后当outputlist
为空终于打到C1
和 C2
在
C3
firstMultiple
是只读的。then firstMultiple // "C3"
在
C1
和 C2
, firstMultiple
是 bound然后阅读,注意我说 bound
不是 set
或 mutated
let firstMultiple = nextInput * factor // "C1"
[nextInput], inputList.Tail, firstMultiple // "C2"
在
C1
firstMultiple
与 firstMultiple
不是同一个变量在 A
或 B
或 C3
,它是一个新变量。所以当你认为它正在改变 firstMultiple
在 C3
的位置它不是。如果您的示例转换是正确的,那么您可以将代码转换为:
let temp = nextInput * factor // "C1"
[nextInput], inputList.Tail, temp // "C2"
或更简单地说
[nextInput], inputList.Tail, (nextInput * factor) // "C2"
在此示例中
firstMultiple
在 C1
和 C2
只是一个重用的变量名,因为阴影是允许的,但不是必需的。编辑
OP 在评论中询问了进一步的问题:
why line "B" takes firstMultiple from "C2" and not from "A".
原因是行
B
是由 B
之后的表达式的结果生成的三个值的元组.所有这些都是为元组创建三个值的表达式。 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
根据匹配结果生成三个元组值的两行是
[nextInput], inputList.Tail, firstMultiple // "C2"
和
newOutputList, remInputList, firstMultiple
专线
B
不是 firstMultiple
的函数作为参数,例如let myFunc firstMultiple = ...
它是一个匹配函数,返回三个值的元组
let newOutputList, remInputList, firstMultiple = // "B"
firstMultiple
未通过 B
传入但被创建为新变量并绑定(bind)在
C1
let firstMultiple = nextInput * factor // "C1"
然后通过
C2
返回[nextInput], inputList.Tail, firstMultiple // "C2"
关于scope - F#,试图理解表达式的范围——这应该工作吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37404575/