Hacker News new | ask | show | jobs
by Dn_Ab 4318 days ago
pjungwir's example code is actually really close to what you'd see in F#. In F# you'd typically write it in a more functional way with say a recursive inner function but I'll leave it imperative for clarity's sake:

    [<Measure>] type s
    [<Measure>] type m
    [<Measure>] type km  

    let mtokm (x:float<m>) = x * 1.<km>/1000.<m>
   
    //the types of g and ground are inferred
    let drop g ground (x0:float<m>) =
         let mutable x = x0  //type inferred here
         let mutable t = 0.0<s>
         let mutable v = 0.0<m/s> 
         let dt = 0.01<s>

         while x >= ground  do
            v <- v + g*dt
            x <- x + v*dt
            t <- t + dt 
         t  

   > drop -10.0<m/s^2> 0.<m> 50.<m>  
   val it : float<s> = 3.16