F #, trying to understand the scope of expressions - should this work?

This is a very skeletonized version of the financial algorithm. In the real world and the processing of the data presented here, “myFunc” has much more logic, which does not really make sense in this version.

My question is: as I understand it, why use firstMultiplein this block is permissible and reasonable. Why is this needed?

The expression is displayed directly from the line marked Ain the line with the inscription C3, where its value is used. But then in C1and C2- which are (approximately) the same level of "indentation" as C3- are firstMultipleassigned here - it would seem like a changed - ??

I think I don’t understand why I’m C3using it firstMultiple, and C1maybe, apparently, to rewrite it. (The debugger and its launch indicates that this is good).

I hope this question and example can get some idea on how to think about (nested) areas. (Of course, I am glad to have other comments on the design, but keep in mind that this code is extremely devoid of much analysis.) (And I may have made the algorithm illogical in my removal from it, but I am trying to focus on the issue of scope coverage.)

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 
+4
source share
2 answers

Since this example is basically a loop

let rec ListBuilder 
    ...
    ListBuilder

C3 outputlist, , outputlist , C1 C2

C3 firstMultiple .

then firstMultiple  // "C3"

C1 C2, firstMultiple bound, , , bound not 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 at C1 C2 - - , .

:

"B" firstMultiple "C2", "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 C1

let firstMultiple = nextInput * factor      // "C1"

C2

[nextInput], inputList.Tail, firstMultiple  // "C2"
+3

. , , . . . .

x , :

let testValue (l: int list) =
    let x = l.[0]
    printfn "%A" x
    do
        let x= l.[1]
        printfn "%A" x
    printfn "%A" x
    let x = l.[2]
    printfn "%A" x 

x :

let testValue2 (l: int list) =
    let mutable x = l.[0]
    printfn "%A" x
    x <- l.[1]
    printfn "%A" x

testValue [1;2;3]

+2

All Articles