trying to understand In which order below f# code is executed and how x%y is evaluated
Function converts a time duration given as a number of hours into a triple comprised of weeks, days and hours.
let hours2we开发者_JAVA技巧eks (h : int) =
let divAndRem x y = (x/y, x%y)
let (w, h) = divAndRem h (7*24)
let (d, h) = divAndRem h 24
(w, d, h)
val hours2weeks : int -> int * int * int
> hours2weeks 1728
val it : int * int * int = (10, 2, 0)
>
You can trace the function execution by reducing the expression step-by-step (this is a very useful way to understanding execution that comes from Haskell and is called computation by calculation).
When you call a function:
hours2weeks 1728
F# evaluates the arguments and then starts evaluating the body:
let (w, h) = divAndRem 1728 (7*24)
let (d, h) = divAndRem 1728 24
(w, d, h)
It starts evaluating the argument of let. First it evaluates arguments of divAndRem
let (w, h) = divAndRem 1728 168
let (d, h) = divAndRem 1728 24
(w, d, h)
and then it calls the divAndRem function with the specified arguments:
let (w, h) = (1728/168, 1728%168)
let (d, h) = divAndRem h 24
(w, d, h)
The body of `divAndRem is evaluated and it gives a tuple with two numbers:
let (w, h) = (10, 48)
let (d, h) = divAndRem h 24
(w, d, h)
Then F# assigns the values to variables and continues:
let (d, h) = divAndRem 48 24
(10, d, h)
The second call to divAndRem is evaluated similarly:
let (d, h) = (2, 0)
(10, d, h)
So you get:
(10, 2, 0)
Now you can use this step-by-step evaluation to see that the 0 value in the result comes from the evaluation of % in the second divAndRem call and that the value 48 (result of the first % call) was needed to make the second divAndRem call.
加载中,请稍侯......
精彩评论