Monday, December 17, 2012

Refactored Mutual Recursion in F#

(continued from phosphorescence: Mutual Recursion in F#)

  • Defining (n -1) as predecessor function
  • Using Pipe-backward operator

"Tarai and laziest tarai" case in F# becomes more readable.
module MutualRecursion =
  let pred n = n - 1
  let rec laziestTarai x y z'x z'y z'z =
    if y < x then
      laziestTarai
        <| tarai (pred x) y (tarai z'x z'y z'z)
        <| tarai (pred y) (tarai z'x z'y z'z) x
        <| (pred <| tarai z'x z'y z'z)
        <| x
        <| y
    else y
  and tarai x y z =
    if y < x then
      laziestTarai
        <| tarai (pred x) y z
        <| tarai (pred y) z x
        <| (pred z)
        <| x
        <| y
    else y

No comments:

Post a Comment