• If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • Buried in cloud files? We can help with Spring cleaning!

    Whether you use Dropbox, Drive, G-Suite, OneDrive, Gmail, Slack, Notion, or all of the above, Dokkio will organize your files for you. Try Dokkio (from the makers of PBworks) for free today.

  • Dokkio (from the makers of PBworks) was #2 on Product Hunt! Check out what people are saying by clicking here.


SI Units

Page history last edited by PBworks 14 years, 8 months ago

SI Units


This one-file module enforces Standard International unit types. This means that it will not allow a developer to add a time with a length or subtract a volume from a weight. It can also convert units from one prefix to another, i.e. km to cm.


Download version 0.1 here


The module has a structure, si-unit, that can only be accessed from within the module. It is used to store the information for the other derivative SI units (length, weight, temperature, etc.) The si-unit structure has quantity, prefix and suffix fields.


Type annotations are indicated by a double colon ("::") after the variable name, i.e. variable::variable-type



Unit Prefixes




si-unit Structure


si-unit-quantity x::si-unit


Returns the quantity field value of the si-unit x.


si-unit-prefix x::si-unit


Returns the prefix field value of the si-unit x.


si-unit-suffix x::si-unit


Returns the suffix field value of the si-unit x.




make-si-length q::real unit::symbol


Creates an si-unit structure that has a quantity of q, a prefix of unit and a suffix of "m". When unit is 'nil


make-si-mass q::real unit::symbol



make-si-time q::real unit::symbol


make-si-current q::real unit::symbol


make-si-temp q::real unit::symbol


make-si-amount q::real unit::symbol


make-si-lumint q::real unit::symbol




The arguments for all math functions can be numbers or si-units or a mix of the two.


.+ x y


.- x y


.* x y


./ x y




convert-to x::si-unit new-unit::symbol


convert-to! x::si-unit new-unit


Destructive version of convert-to.


si->string x




Example 1


Marvin the Martian rover is measuring the distance from one rock to another. He can measure accurately down to 1 mm and up to 1 m.


Without using the SI module:


(define (distance x1 y1 x2 y2) ; square root of ((x2 - x1) ^ 2 + (y2 - y1) ^ 2)


(sqrt (+ (expt (- x2 x1) 2)

(expt (- y2 y1) 2))))

(distance 2.0 1.0 0.43 2.4)


The process of conversion must be written in by hand otherwise the results are inaccurate. Also, how do we know that we're getting 2.4 m and not 2.4 mm?


Using the SI module:

(define (distance x1 y1 x2 y2) ; square root of ((x2 - x1) ^ 2 + (y2 - y1) ^ 2)


(sqrt (.+ (expt (.- x2 x1) 2)

(expt (.- y2 y1) 2))))

(distance (make-si-length 2.0 'nil) ; 2.0 m
(make-si-length 1.0 'centi) ; 1.0 cm = 0.01 m

(make-si-length 0.43 'nil) ; 0.43 m

(make-si-length 2.4 'milli) ; 2.4 mm = 0.0024 m



The difference is that the units are specified and that a different function is used for mathematical operations. Instead of +, we use .+


Example 2


Comments (0)

You don't have permission to comment on this page.