F#: Ballistics, Rocketry and Research 4/11/2009 posting

Quiz answers

Is the “let” keyword only used for values?

  • No, the “let” keyword is used for values and functions

Is the “let” keyword only used for functions?

  • No, functions and values are treated the same way in F#

If I write the line of code: let TimeBump = 1.0<ft>*3.0<m>, will I get a context error indication?

  • Yes, if the units are set in F#, then ft cannot dimensionally be used with meters.  F# does not support the dimensional analysis by default, there is some work required to make it happen.


We will drive back to the Estes code in a later entry, for now, the dimensional analysis problem and solution in F# is more fully described.


Back to reading Chris Smith’s code from his well written codei BurnedLand, the previous post discussed the F# class: enviroment.fs.  One of the quiz questions ask about the use of units and dimensional analysis.  Let’s take a look at the way Chris set-up the units of measurement, Chris used an efficient process.  To define a unit of measurement it is done by using the syntax, in this case the units such as m represent meters, but the code only knows that it is an m unit of some sort that is different than an f, which might be a unit of measurement about 1/3 of a meter used in the US.  The compiler examines the calculations and uses certain rules of dimensional analysis.  For now, to keep it simple, if you attempt to add the unit m and the unit f, you will get a syntax error indication in your code.  From a dimensional analysis it would make sense to have meters/feet if you are doing conversions to either meters or feet.  Units of measurement can be squared as well, for instance the following, this is an example from the F# Code specification, page 138:

  • let earthGravity = 9.81f<m/s^2>
    • Here the unmutable variable earthGravith utilizes the units m and the units s, we know that the unit m stands for meters and the s stands for seconds
  • let atmosphere = 101325.0<N m^-2>
    • This is an atmosphere at sea level on Earth, as you can see the units for meters is –2, and this could be rewritten as <N/m^2> depending on style considerations
    • There is no run-time performance hit since the units are stripped off during the compilation process, they are used only during the development process

The ability to show units is big help in making sure that computer scientists, who are skilled at organizing 1’s and 0’s, keep in mind that their work may need to be reviewed and used by non-Computer Scientists.  The non-Computer Scientists may be an Electrical Engineer who thinks in Ohms law, or the Mechanical Engineer who thinks about stresses and coefficients of restitution, which all require the ability to read the code in a manner that they think.

Here is the code from the Chris Smith’s BurnedLand

 // Define custom units of measure. 
 type m 
 type s 
 type kg 
 type joules 
 type degs 
 type rads 
 // Conversion routines for degrees and radians 
 open System 
 let DegsToRads (angle : float<degs>) = angle * Math.PI / 180.0 / 1.0<degs>
 let RadsToDegs (rads  : float<rads>) = rads  * 180.0 / Math.PI / 1.0<rads> 

Technorati Tags: F#,Dimensional analysis,Orbital Mechanics,Celestial Mechanics,Fun