简体   繁体   中英

F# do let execution order

I'm new to F# so this block of code seems strange to me

let randomTest avgWait avgBusyTime numExp numClients labsRules =
    let clients, _ = mkClientsAndLabs numClients labsRules 
    doTest [for i in 0..numClients-1 -> randomTestClient clients i avgWait avgBusyTime numExp  ]

do let clients, _ = mkClientsAndLabs 5 [rulesA; rulesB] 
   doTest [scheduledClient clients 0 [(0, 500, A)];     // Request a lab at the very start, use for "A" for 0.5 seconds
           scheduledClient clients 1 [(200, 300, Mix (Mix (A,Mix (A,A)),B))] ;   // Request after 0.2s, release 0.3s later.

           scheduledClient clients 2 [(300, 200, Mix (A,Mix (A,A)))];   // These three will all be waiting for a lab.
           scheduledClient clients 3 [(400, 200, Mix (A,A))];           // Client 2 should include the others as guests.
           scheduledClient clients 4 [(400, 200, A)]
          ]

What I am unsure is the do let declaration - it obviously is declared after randomTest yet randomTest can still call that function. What is the order of this code execution?

The way it's written may be confusing. There is no such thing as a do let declaration. In fact it's a whole do {code} block with a let binding inside the {code} . That means it's not a function declaration, a do block is just a code to be executed, it does not declare functions or values.

Should be easier to read like this:

do 
   let clients, _ = mkClientsAndLabs 5 [rulesA; rulesB] 
   doTest [scheduledClient clients 0 [(0, 500, A)];     // Request a lab at the very start, use for "A" for 0.5 seconds
           scheduledClient clients 1 [(200, 300, Mix (Mix (A,Mix (A,A)),B))] ;   // Request after 0.2s, release 0.3s later.

           scheduledClient clients 2 [(300, 200, Mix (A,Mix (A,A)))];   // These three will all be waiting for a lab.
           scheduledClient clients 3 [(400, 200, Mix (A,A))];           // Client 2 should include the others as guests.
           scheduledClient clients 4 [(400, 200, A)]
          ]

So the order of execution is first let randomTest ... , then the do block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM