# This is a line comment. It can span over# multiple lines# statements can be separated by newlines or semicolons121;2# 1. Importsuseprelude# This is not necessary. The 'prelude'# module will always be loaded upon startupuseunits::stoney# Load a specific module# 2. Numbers12345# integer notation12_345# optional decimal separators0.234# floating point notation.234# without the leading zero1.234e15# scientific notation1.234e+151e-91.0e-90x2A# hexadecimal0o52# octal0b101010# binaryNaN# Not a numberinf# Infinity# 3. Simple expressions3+(4-3)# Addition and subtraction1920/16*9# Multiplication, division1920÷16×9# Unicode-style, '·' or '⋅' works as well2pi# Whitespace is implicit multiplicationmeterpersecond# 'per' keyword can be used for division2^3# Exponentiation2**3# Python-style2³# Unicode exponents2^-3# Negative exponentsmod(17,4)# Modulo3in->cm# Unit conversion, can also be → or ➞3intocm# Unit conversion with the 'to' keywordcos(pi/3+pi)# Call mathematical functionspi/3+pi|>cos# Same, 'arg |> f' is equivalent to 'f(arg)'# The '|>' operator has the lowest precedence# which makes it very useful for interactive# terminals (press up-arrow, and add '|> f')_# Result of last calculation, also 'ans'# 4. Constantsletn=4# Simple numerical constantletq1=2m/s# Right hand side can be any expressionletq2:Velocity=2m/s# With optional type annotationletq3:Length/Time=2m/s# more complex type annotation# 5. Function definitionsfnfoo(z:Scalar)->Scalar=2*z+3# A simple functionfnspeed(len:Length,dur:Time)->Velocity=len/dur# Two parametersfnmy_sqrt<T:Dim>(q:T^2)->T=q^(1/2)# A generic functionfnis_non_negative(x:Scalar)->Bool=x≥0# Returns a boolfnpower_4(x:Scalar)=z# A function with local variableswherey=x*xandz=y*y# 6. Dimension definitionsdimensionFame# A new base dimensiondimensionDeceleration=Length/Time^2# A new derived dimension# 7. Unit definitions@aliases(quorks)# Optional aliases-decoratorunitquork=0.35meter# A new derived unit@metric_prefixes# Optional decorator to allow 'milliclonk', etc.@aliases(ck:short)# short aliases can be used with short prefixes (mck)unitclonk:Time=0.2seconds# Optional type annotation@metric_prefixes@aliases(wh:short)unitwarhol:Fame# New base unit for the "Fame" dimensionunitthing# New base unit with automatically generated# base dimension "Thing"# 8. Conditionalsfnbump(x:Scalar)->Scalar=# The construct 'if <cond> then <expr> else <expr>'ifx>=0&&x<=1# is an expression, not a statement. It can spanthen1# multiple lines.else0# 9. Proceduresprint(2kilowarhol)# Print the value of an expressionprint("hello world")# Print a messageprint("value of pi = {pi}")# String interpolationprint("sqrt(10) = {sqrt(10)}")# Expressions in string interpolationprint("value of π ≈ {π:.3}")# Format specifiersassert(1yard<1meter)# Assertionassert_eq(1ft,12in)# Assert that two quantities are equalassert_eq(1yd,1m,10cm)# Assert that two quantities are equal, up to# the given precisiontype(2m/s)# Print the type of an expression# 10. StructsstructElement{# Define a structname:String,atomic_number:Scalar,density:MassDensity,}lethydrogen=Element{# Instantiate itname:"Hydrogen",atomic_number:1,density:0.08988g/L,}hydrogen.density# Access the field of a struct