hd-lang

Tour · 3 of 18

Functions

The program on the right works out a shipping price in three small steps.

#Parameters and results

fn add_fee(cents: i32, fee: i32) -> i32:
    cents + fee

Every parameter and the result have a type. The last expression is the result, so there is no return.

#if gives a value

rate := if express: +40 else: +20

if picks one of two values, and both must have the same type.

#Pipes

weight_grams
    |> charge(_, rate)
    |> add_fee(_, 99)

|> passes each result to the next step, and _ marks where it goes. This pipe means add_fee(charge(weight_grams, rate), 99).

Run it. 1200 grams by express costs 12 * 40 + 99, so it prints 579.

Try it: change |> charge(_, rate) to |> charge(rate) and press Run.

Run: Ctrl+Enter · Pages: Alt+← Alt+→