Skip to content

Commit 9073f19

Browse files
committed
Major documentation update
1 parent 8ce1eea commit 9073f19

25 files changed

+270
-163
lines changed

book/src/SUMMARY.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@
3333
- [Conversions functions](./conversion-functions.md)
3434
- [Function definitions](./function-definitions.md)
3535
- [Conditionals](./conditionals.md)
36+
- [Lists](./lists.md)
37+
- [Structs](./structs.md)
3638
- [Date and time](./date-and-time.md)
3739
- [Printing, testing, debugging](./procedures.md)
38-
- [Structs](./structs.md)
3940
- [Advanced](./advanced.md)
4041
- [Dimension definitions](./dimension-definitions.md)
4142
- [Unit definitions](./unit-definitions.md)

book/src/conditionals.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ where `<cond>` is a condition that evaluates to a Boolean value, like
1111
For example, you can defined a simple step function using
1212

1313
```nbt
14-
fn step(x: Scalar) -> Scalar = if x < 0 then 0 else 1
14+
fn step(x) = if x < 0 then 0 else 1
1515
```

book/src/conversion-functions.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@ now() -> tz("Asia/Kathmandu")
3030
# Convert a number to its hexadecimal representation
3131
2^31-1 -> hex
3232
33-
# Convert a number to a custom base
34-
42 -> base(16)
35-
3633
# Convert a code point number to a character
3734
0x2764 -> chr
3835
@@ -44,5 +41,5 @@ now() -> tz("Asia/Kathmandu")
4441
"vier bis elf weiße Querbänder" -> lowercase
4542
```
4643

47-
Note that the `tz(…)` and `base(…)` calls above return *functions*, i.e. the right hand side of
44+
Note that the `tz(…)` call above *returns a function*, i.e. the right hand side of
4845
the conversion operator is still a function.

book/src/example-factorial.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
<!-- This file is autogenerated! Do not modify it -->
22

33
# Factorial
4-
<a href="https://numbat.dev/?q=%23+Naive+factorial+implementation+to+showcase+recursive%0A%23+functions+and+conditionals.%0A%0Afn+factorial%28n%3A+Scalar%29+-%3E+Scalar+%3D%0A++if+n+%3C+1%0A++++then+1%0A++++else+n+%C3%97+factorial%28n+-+1%29%0A%0A%23+Compare+result+with+the+builtin+factorial+operator%0Aassert_eq%28factorial%2810%29%2C+10%21%29%0A"><i class="fa fa-play"></i> Run this example</a>
4+
<a href="https://numbat.dev/?q=%23+Naive+factorial+implementation+to+showcase+recursive%0A%23+functions+and+conditionals.%0A%0Afn+factorial%28n%29+%3D%0A++if+n+%3C+1%0A++++then+1%0A++++else+n+%C3%97+factorial%28n+-+1%29%0A%0A%23+Compare+result+with+the+builtin+factorial+operator%0Aassert_eq%28factorial%2810%29%2C+10%21%29%0A"><i class="fa fa-play"></i> Run this example</a>
55

66
``` numbat
77
# Naive factorial implementation to showcase recursive
88
# functions and conditionals.
99
10-
fn factorial(n: Scalar) -> Scalar =
10+
fn factorial(n) =
1111
if n < 1
1212
then 1
1313
else n × factorial(n - 1)

book/src/example-population_growth.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
<!-- This file is autogenerated! Do not modify it -->
22

33
# Population growth
4-
<a href="https://numbat.dev/?q=%23+Exponential+model+for+population+growth%0A%0Alet+initial_population+%3D+50_000+people%0Alet+growth_rate+%3D+2%25+per+year%0A%0Afn+predict_population%28t%3A+Time%29+%3D%0A++++initial_population+%C3%97+e%5E%28growth_rate%C2%B7t%29+%7C%3E+round%0A%0Aprint%28%22Population+in++20+years%3A+%7Bpredict_population%2820+years%29%7D%22%29%0Aprint%28%22Population+in+100+years%3A+%7Bpredict_population%281+century%29%7D%22%29%0A"><i class="fa fa-play"></i> Run this example</a>
4+
<a href="https://numbat.dev/?q=%23+Exponential+model+for+population+growth%0A%0Alet+initial_population+%3D+50_000+people%0Alet+growth_rate+%3D+2%25+per+year%0A%0Afn+predict_population%28t%29+%3D%0A++++initial_population+%C3%97+e%5E%28growth_rate%C2%B7t%29+%7C%3E+round%0A%0Aprint%28%22Population+in++20+years%3A+%7Bpredict_population%2820+years%29%7D%22%29%0Aprint%28%22Population+in+100+years%3A+%7Bpredict_population%281+century%29%7D%22%29%0A"><i class="fa fa-play"></i> Run this example</a>
55

66
``` numbat
77
# Exponential model for population growth
88
99
let initial_population = 50_000 people
1010
let growth_rate = 2% per year
1111
12-
fn predict_population(t: Time) =
12+
fn predict_population(t) =
1313
initial_population × e^(growth_rate·t) |> round
1414
1515
print("Population in 20 years: {predict_population(20 years)}")

book/src/example-xkcd_2585.md

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!-- This file is autogenerated! Do not modify it -->
22

33
# XKCD 2585
4-
<a href="https://numbat.dev/?q=%23+Rounding%0A%23%0A%23+https%3A%2F%2Fxkcd.com%2F2585%2F%0A%0A17+mph%0A%0Aans+-%3E+meters%2Fsec++++%7C%3E+round%0Aans+-%3E+knots+++++++++%7C%3E+round%0Aans+-%3E+fathoms%2Fsec+++%7C%3E+round%0Aans+-%3E+furlongs%2Fmin++%7C%3E+round%0Aans+-%3E+fathoms%2Fsec+++%7C%3E+round%0Aans+-%3E+kph+++++++++++%7C%3E+round%0Aans+-%3E+knots+++++++++%7C%3E+round%0Aans+-%3E+kph+++++++++++%7C%3E+round%0Aans+-%3E+furlongs%2Fhour+%7C%3E+round%0Aans+-%3E+mph+++++++++++%7C%3E+round%0Aans+-%3E+m%2Fs+++++++++++%7C%3E+round%0Aans+-%3E+furlongs%2Fmin++%7C%3E+round%0Aans+-%3E+yards%2Fsec+++++%7C%3E+round%0Aans+-%3E+fathoms%2Fsec+++%7C%3E+round%0Aans+-%3E+m%2Fs+++++++++++%7C%3E+round%0Aans+-%3E+mph+++++++++++%7C%3E+round%0Aans+-%3E+furlongs%2Fmin++%7C%3E+round%0Aans+-%3E+knots+++++++++%7C%3E+round%0Aans+-%3E+yards%2Fsec+++++%7C%3E+round%0Aans+-%3E+fathoms%2Fsec+++%7C%3E+round%0Aans+-%3E+knots+++++++++%7C%3E+round%0Aans+-%3E+furlongs%2Fmin++%7C%3E+round%0Aans+-%3E+mph+++++++++++%7C%3E+round%0A%0Aprint%28%22I+can+ride+my+bike+at+%7Bans%7D.%22%29%0Aprint%28%22If+you+round.%22%29%0A"><i class="fa fa-play"></i> Run this example</a>
4+
<a href="https://numbat.dev/?q=%23+Rounding%0A%23%0A%23+https%3A%2F%2Fxkcd.com%2F2585%2F%0A%0A17+mph%0A%0Aans+-%3E+meters%2Fsec++++-%3E+round%0Aans+-%3E+knots+++++++++-%3E+round%0Aans+-%3E+fathoms%2Fsec+++-%3E+round%0Aans+-%3E+furlongs%2Fmin++-%3E+round%0Aans+-%3E+fathoms%2Fsec+++-%3E+round%0Aans+-%3E+kph+++++++++++-%3E+round%0Aans+-%3E+knots+++++++++-%3E+round%0Aans+-%3E+kph+++++++++++-%3E+round%0Aans+-%3E+furlongs%2Fhour+-%3E+round%0Aans+-%3E+mph+++++++++++-%3E+round%0Aans+-%3E+m%2Fs+++++++++++-%3E+round%0Aans+-%3E+furlongs%2Fmin++-%3E+round%0Aans+-%3E+yards%2Fsec+++++-%3E+round%0Aans+-%3E+fathoms%2Fsec+++-%3E+round%0Aans+-%3E+m%2Fs+++++++++++-%3E+round%0Aans+-%3E+mph+++++++++++-%3E+round%0Aans+-%3E+furlongs%2Fmin++-%3E+round%0Aans+-%3E+knots+++++++++-%3E+round%0Aans+-%3E+yards%2Fsec+++++-%3E+round%0Aans+-%3E+fathoms%2Fsec+++-%3E+round%0Aans+-%3E+knots+++++++++-%3E+round%0Aans+-%3E+furlongs%2Fmin++-%3E+round%0Aans+-%3E+mph+++++++++++-%3E+round%0A%0Aprint%28%22I+can+ride+my+bike+at+%7Bans%7D.%22%29%0Aprint%28%22If+you+round.%22%29%0A"><i class="fa fa-play"></i> Run this example</a>
55

66
``` numbat
77
# Rounding
@@ -10,29 +10,29 @@
1010
1111
17 mph
1212
13-
ans -> meters/sec |> round
14-
ans -> knots |> round
15-
ans -> fathoms/sec |> round
16-
ans -> furlongs/min |> round
17-
ans -> fathoms/sec |> round
18-
ans -> kph |> round
19-
ans -> knots |> round
20-
ans -> kph |> round
21-
ans -> furlongs/hour |> round
22-
ans -> mph |> round
23-
ans -> m/s |> round
24-
ans -> furlongs/min |> round
25-
ans -> yards/sec |> round
26-
ans -> fathoms/sec |> round
27-
ans -> m/s |> round
28-
ans -> mph |> round
29-
ans -> furlongs/min |> round
30-
ans -> knots |> round
31-
ans -> yards/sec |> round
32-
ans -> fathoms/sec |> round
33-
ans -> knots |> round
34-
ans -> furlongs/min |> round
35-
ans -> mph |> round
13+
ans -> meters/sec -> round
14+
ans -> knots -> round
15+
ans -> fathoms/sec -> round
16+
ans -> furlongs/min -> round
17+
ans -> fathoms/sec -> round
18+
ans -> kph -> round
19+
ans -> knots -> round
20+
ans -> kph -> round
21+
ans -> furlongs/hour -> round
22+
ans -> mph -> round
23+
ans -> m/s -> round
24+
ans -> furlongs/min -> round
25+
ans -> yards/sec -> round
26+
ans -> fathoms/sec -> round
27+
ans -> m/s -> round
28+
ans -> mph -> round
29+
ans -> furlongs/min -> round
30+
ans -> knots -> round
31+
ans -> yards/sec -> round
32+
ans -> fathoms/sec -> round
33+
ans -> knots -> round
34+
ans -> furlongs/min -> round
35+
ans -> mph -> round
3636
3737
print("I can ride my bike at {ans}.")
3838
print("If you round.")

book/src/example-xkcd_2812.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<!-- This file is autogenerated! Do not modify it -->
22

33
# XKCD 2812
4-
<a href="https://numbat.dev/?q=%23+Solar+panel+placement%0A%23%0A%23+Solar+energy+tip%3A+To+maximize+sun+exposure%2C+always%0A%23+orient+your+panels+downward+and+install+them+on+the%0A%23+surface+of+the+sun.%0A%23%0A%23+https%3A%2F%2Fxkcd.com%2F2812%2F%0A%23%0A%23+%5B1%5D+https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FSolar_luminosity%0A%23+%5B2%5D+https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FSun%0A%0Aunit+%24%3A+Money%0A%0Alet+net_metering_rate+%3D+%24+0.20+%2F+kWh%0Alet+panel_area+%3D+1+m%C2%B2%0Alet+panel_efficiency+%3D+20+%25%0A%0Afn+savings%28i%3A+Irradiance%29+-%3E+Money+%2F+Time+%3D%0A++++net_metering_rate+%C3%97+i+%C3%97+panel_area+%C3%97+panel_efficiency+-%3E+%24%2Fyear%0A%0Aprint%28%22Option+A%3A+On+the+roof%2C+south+facing%22%29%0A%0Alet+savings_a+%3D+savings%284+kWh%2Fm%C2%B2%2Fday%29%0A%0Aprint%28savings_a+%7C%3E+round%29%0A%0Aprint%28%29%0Aprint%28%22Option+B%3A+On+the+sun%2C+downward+facing%22%29%0A%0Adimension+Luminosity+%3D+Power%0A%0Alet+sun_luminosity%3A+Luminosity+%3D+3.828e26+W++%23+%5B1%5D%0Alet+sun_area%3A+Area+%3D+6.09e12+km%5E2++++++++++++%23+%5B2%5D%0A%0Alet+savings_b+%3D+savings%28sun_luminosity+%2F+sun_area%29%0A%0Aprint%28savings_b+%7C%3E+round%29%0A"><i class="fa fa-play"></i> Run this example</a>
4+
<a href="https://numbat.dev/?q=%23+Solar+panel+placement%0A%23%0A%23+Solar+energy+tip%3A+To+maximize+sun+exposure%2C+always%0A%23+orient+your+panels+downward+and+install+them+on+the%0A%23+surface+of+the+sun.%0A%23%0A%23+https%3A%2F%2Fxkcd.com%2F2812%2F%0A%23%0A%23+%5B1%5D+https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FSolar_luminosity%0A%23+%5B2%5D+https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FSun%0A%0Alet+net_metering_rate+%3D+%24+0.20+%2F+kWh%0Alet+panel_area+%3D+1+m%C2%B2%0Alet+panel_efficiency+%3D+20+%25%0A%0Afn+savings%28i%3A+Irradiance%29+-%3E+Money+%2F+Time+%3D%0A++++net_metering_rate+%C3%97+i+%C3%97+panel_area+%C3%97+panel_efficiency+-%3E+%24%2Fyear%0A%0Aprint%28%22Option+A%3A+On+the+roof%2C+south+facing%22%29%0A%0Alet+savings_a+%3D+savings%284+kWh%2Fm%C2%B2%2Fday%29%0A%0Aprint%28savings_a+%7C%3E+round%29%0A%0Aprint%28%29%0Aprint%28%22Option+B%3A+On+the+sun%2C+downward+facing%22%29%0A%0Adimension+Luminosity+%3D+Power%0A%0Alet+sun_luminosity%3A+Luminosity+%3D+3.828e26+W++%23+%5B1%5D%0Alet+sun_area%3A+Area+%3D+6.09e12+km%5E2++++++++++++%23+%5B2%5D%0A%0Alet+savings_b+%3D+savings%28sun_luminosity+%2F+sun_area%29%0A%0Aprint%28savings_b+%7C%3E+round%29%0A"><i class="fa fa-play"></i> Run this example</a>
55

66
``` numbat
77
# Solar panel placement
@@ -15,8 +15,6 @@
1515
# [1] https://en.wikipedia.org/wiki/Solar_luminosity
1616
# [2] https://en.wikipedia.org/wiki/Sun
1717
18-
unit $: Money
19-
2018
let net_metering_rate = $ 0.20 / kWh
2119
let panel_area = 1 m²
2220
let panel_efficiency = 20 %

book/src/function-definitions.md

Lines changed: 77 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,48 +9,90 @@ fn max_distance(v: Velocity, θ: Angle) -> Length = v² · sin(2 θ) / g0
99
```
1010

1111
This exemplary function computes the maximum distance of a projectile under the
12-
influence of Earths gravity. It takes two parameters (The initial velocity `v` and
12+
influence of Earths gravity. It takes two parameters (the initial velocity `v` and
1313
the launch angle `θ`), which are both annotated with their corresponding physical
1414
dimension (their type). The function returns a distance, and so the return type
1515
is specified as `Length`.
1616

1717
## Type inference
1818

19-
The return type annotation may be omitted, but it is often desirable to add it
20-
for better readability of the code and in order to catch potential errors.
21-
22-
The parameter types can also (sometimes) be omitted, in which case Numbat tries
23-
to infer their type. However, this often leads to overly generic function
24-
signatures. For example, consider the following function to compute the kinetic
25-
energy of a massive object in motion:
19+
Numbat has a powerful type inference system, which is able to infer missing types
20+
when they are not explicitly specified. For example, consider the following function
21+
definition for the breaking distance of a car, given its velocity `v`:
22+
```nbt
23+
fn breaking_distance(v) = v t_reaction + v² / 2 µ g0
24+
where t_reaction = 1 s # driver reaction time
25+
and µ = 0.7 # coefficient of friction
26+
```
27+
If you enter this function into the Numbat REPL, you will see that all types are filled
28+
in automatically:
29+
```nbt
30+
fn breaking_distance(v: Velocity) -> Length = v × t_reaction + (v² / (2 µ × g0))
31+
where t_reaction: Time = 1 second
32+
and µ: Scalar = 0.7
33+
```
34+
In particular, note that the type of the function argument `v` is correctly inferred as
35+
`Velocity`, and the return type is `Length`.
36+
37+
> **Note**: This is possible because the types of `t_reaction`, `µ`, and `g0` (gravitational acceleration)
38+
> are known. The `+` operator imposes a *constraint* on the types: two quantities can
39+
> only be added if their physical dimension is the same. The type inference algorithm
40+
> records constraints like this, and then tries to find a solution that satisfies all
41+
> all of them. In this case, only a single equation needs to be solved:
42+
> ```
43+
> type(v) × type(t_reaction) = type(v)² / (type(µ) × type(g0) )
44+
> type(v) × Time = type(v)² / ( 1 × Length / Time²)
45+
> ```
46+
> which has the solution `type(v) = Length / Time = Velocity`. Note that this also
47+
> works if there are multiple constraints on the types. In fact, type inference is
48+
> always decidable.
49+
50+
The fact that it is *possible* to omit type annotations does not mean that it is always
51+
a good idea to do so.
52+
Type annotations can help to make the code more readable and can also help to catch
53+
errors earlier.
54+
55+
In some cases, type inference will also lead to function types that are overly generic.
56+
For example, consider the following function to compute the kinetic energy of a massive
57+
object in motion:
2658
2759
```nbt
2860
fn kinetic_energy(mass, speed) = 1/2 * mass * speed^2
2961
```
3062
31-
Without any type annotations, this function has an overly generic type where
32-
`mass` and `speed` can have arbitrary dimensions (and the return type is
33-
`type(mass) * type(speed)^2`). So for this case, it is probably better to add
34-
parameter and return types.
63+
In the absence of any type annotations, this function has an overly generic type where
64+
`mass` and `speed` can have arbitrary dimensions (but the return type is constrained
65+
accordingly):
66+
```nbt
67+
fn kinetic_energy<A: Dim, B: Dim>(mass: A, speed: B) -> A × B² = …
68+
```
69+
In this example, it would be better to specify the types of `mass` and `speed`
70+
explicitly (`Mass`, `Velocity`). The return type can then be inferred (`Energy`).
71+
It is still valuable to specify it explicitly, in order to ensure there are no
72+
mistakes in the function implementation.
3573

3674
## Generic functions
3775

38-
Sometimes however, it *is* useful to write generic functions. For example, consider
76+
Sometimes it is useful to write generic functions. For example, consider
3977
`max(a, b)` — a function that returns the larger of the two arguments. We might
4078
want to use that function with *dimensionful* arguments such as `max(1 m, 1 yd)`.
4179
To define such a generic function, you can introduce *type parameters* in angle
4280
brackets:
4381

4482
```nbt
45-
fn max<T>(a: T, b: T) -> T = if a > b then a else b
83+
fn max<D: Dim>(a: D, b: D) -> D =
84+
if a > b then a else b
4685
```
4786

4887
This function signature tells us that `max` takes two arguments of *arbitrary*
49-
type `T` (but they need to match!), and returns a quantity of the same type `T`.
88+
dimension type `D` (but they need to match!), and returns a quantity of the same
89+
type `D`. The `D: Dim` syntax is a *type constraint* (or bound) that ensures that
90+
`D` is a dimension type (`Scalar`, `Length`, `Velocity`, etc), and not something
91+
like `Bool` or `DateTime`.
5092

51-
Note that you can perform the usual operations with type parameters, such as
52-
multiplying/dividing them with other types, or raising to rational powers. For
53-
example, consider this cube-root function
93+
Note that you can perform the usual operations with (dimension) type parameters,
94+
such as multiplying / dividing them with other types, or raising to rational powers.
95+
For example, consider this cube-root function
5496

5597
```nbt
5698
fn cube_root<T>(x: T^3) -> T = x^(1/3)
@@ -62,17 +104,27 @@ argument (`cube_root(1 liter) == 10 cm`).
62104
Note: `cube_root` can also be defined as `fn cube_root<T>(x: T) -> T^(1/3)`,
63105
which is equivalent to the definition above.
64106

65-
## Recursive functions
107+
Functions can also be generic over *all* types, not just dimension types. In this case,
108+
no type constraints are needed. For example:
109+
```nbt
110+
fn second_element<A>(xs: List<A>) -> A =
111+
head(tail(xs))
112+
113+
second_element([10 cm, 2 m, 3 inch]) # returns 2 m
114+
second_element(["a", "b", "c"]) # returns "b"
115+
```
116+
117+
Note that the type annotations for all examples in this section are optional and
118+
can also be inferred.
66119

67-
It is also possible to define recursive functions. In order to do so, you
68-
currently need to specify the return type — as the type signature can not
69-
(yet) be inferred otherwise.
120+
## Recursive functions
70121

71-
For example, a naive recursive implementation to compute Fibonacci numbers
72-
in Numbat looks like this:
122+
It is also possible to define recursive functions. For example, a naive
123+
recursive implementation to compute Fibonacci numbers in Numbat looks like
124+
this:
73125

74126
```nbt
75-
fn fib(n: Scalar) -> Scalar =
127+
fn fib(n) =
76128
if n ≤ 2
77129
then 1
78130
else fib(n - 2) + fib(n - 1)

book/src/introduction.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,5 @@ The real strength of Numbat, however, is to perform calculations with physical u
5252
= 1.87855 eV [Energy]
5353
```
5454

55-
Read the [tutorial](./tutorial.md) to learn more about the language. Or jump directly
56-
to the [syntax reference](./example-numbat_syntax.md).
55+
Read the [tutorial](./tutorial.md) to learn more about the language or look at some [example programs](./examples.md).
56+
You can also jump directly to the [syntax reference](./example-numbat_syntax.md).

book/src/list-functions-other.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ More information [here](https://doc.rust-lang.org/std/primitive.f64.html#method.
3333
fn is_infinite<T: Dim>(n: T) -> Bool
3434
```
3535

36+
### `is_finite`
37+
Returns true if the input is neither infinite nor `NaN`.
38+
39+
```nbt
40+
fn is_finite<T: Dim>(n: T) -> Bool
41+
```
42+
3643
## Quantities
3744

3845
Defined in: `core::quantities`

book/src/list-functions-strings.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,13 @@ Repeat the input string `n` times.
7979
fn str_repeat(a: String, n: Scalar) -> String
8080
```
8181

82+
### `base`
83+
Convert a number to the given base. Example: `42 |> base(16)`.
84+
85+
```nbt
86+
fn base(b: Scalar, x: Scalar) -> String
87+
```
88+
8289
### `bin`
8390
Get a binary representation of a number. Example: `42 -> bin`.
8491

@@ -107,10 +114,3 @@ Get a hexadecimal representation of a number. Example: `2^31-1 -> hex`.
107114
fn hex(x: Scalar) -> String
108115
```
109116

110-
### `base`
111-
Convert a number to the given base. Example: `42 -> base(16)`.
112-
113-
```nbt
114-
fn base(b: Scalar) -> Fn[(Scalar) -> String]
115-
```
116-

book/src/lists.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Lists
2+
3+
Numbat has a built-in data type for lists. The elements can be of any type, including other lists.
4+
Lists can be created using the `[…]` syntax. For example:
5+
6+
```nbt
7+
[30 cm, 110 cm, 2 m]
8+
["a", "b", "c"]
9+
[[1, 2], [3, 4]]
10+
```
11+
12+
The type of a list is written as `List<T>`, where `T` is the type of the elements. The types of the lists
13+
above are `List<Length>`, `List<String>`, and `List<List<Scalar>>`, respectively.
14+
15+
The standard library provides a [number of functions](./list-functions-lists.md) to work with lists. Some
16+
useful things to do with lists are:
17+
```nbt
18+
# Get the length of a list
19+
len([1, 2, 3]) # returns 3
20+
21+
# Sum all elements of a list:
22+
sum([30 cm, 130 cm, 2 m]) # returns 360 cm
23+
24+
# Get the average of a list:
25+
mean([30 cm, 130 cm, 2 m]) # returns 120 cm
26+
27+
# Filter a list:
28+
filter(is_finite, [20 cm, inf, 1 m]) # returns [20 cm, 1 m]
29+
30+
# Map a function over a list:
31+
map(sqr, [10 cm, 2 m]) # returns [100 cm², 4 m²]
32+
33+
# Generate a range of numbers:
34+
range(1, 5) # returns [1, 2, 3, 4, 5]
35+
36+
# Generate a list of evenly spaced quantities:
37+
linspace(0 m, 1 m, 5) # returns [0 m, 0.25 m, 0.5 m, 0.75 m, 1 m]
38+
```

0 commit comments

Comments
 (0)