scope - F#,试图理解表达式的范围——这应该工作吗?

标签 scope f#

这个问题在这里已经有了答案:





Shadowing and Nested function

(2 个回答)


5年前关闭。




这是金融算法的高度骨架化版本。这里用“myFunc”表示的实际条件和数据处理有更多的逻辑——这在这个版本中并没有真正的意义。

我的问题是:我怎么理解为什么使用 firstMultiple在这个 block 是允许和明智的。为什么它应该起作用?

表达式直接从标记为 A 的行映射。到标记为 C3 的行在哪里使用它的值。但后来在 C1C2 -- 与 C3 的“缩进”级别(大致)相同--这里firstMultiple被分配——似乎它是可变的——??

我想我不明白为什么C3使用 firstMultipleC1似乎可以覆盖它。 (调试器并运行它,表明它很好)。

我希望这个问题和示例可能会引发一些关于如何思考(嵌套)范围的见解。 (当然,我很高兴对设计有其他评论,但请记住,这段代码非常剥离了很多分析。)(我可能在剥离它时使算法不合逻辑,但是我试图专注于范围问题。)

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为空终于打到C1C2
C3 firstMultiple是只读的。
then firstMultiple  // "C3"

C1C2 , firstMultiplebound然后阅读,注意我说 bound不是 setmutated
let firstMultiple = nextInput * factor      // "C1" 
[nextInput], inputList.Tail, firstMultiple  // "C2"

C1 firstMultiplefirstMultiple 不是同一个变量在 ABC3 ,它是一个新变量。所以当你认为它正在改变 firstMultipleC3 的位置它不是。

如果您的示例转换是正确的,那么您可以将代码转换为:
let temp = nextInput * factor      // "C1"
[nextInput], inputList.Tail, temp  // "C2"

或更简单地说
[nextInput], inputList.Tail, (nextInput * factor) // "C2"

在此示例中 firstMultipleC1C2只是一个重用的变量名,因为阴影是允许的,但不是必需的。

编辑

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/

相关文章:

java - 将 `@PathVariable` 注入(inject)请求作用域 bean

c# - 使用 Dispose 注入(inject) transient 作用域

asynchronous - 如果程序立即失败,则 MailboxProcessor 第一个循环无法运行

C++ - 将对象添加到 std::vector,在循环中实例化

javascript - 闭包中变量的性能与函数参数

javascript - jQuery on Change 引用 this.value 和 this

F#模式匹配: Matching functions/lists of subtypes?

f# - 通过函数调用区分联合模式匹配

visual-studio-2010 - 在 Visual Studio 中构建时出现 fsyacc 错误

.net - 非常简单的 F# 表单锁定键盘输入