Add factorial and fibonacci examples
This commit is contained in:
@@ -24,16 +24,25 @@ Then serve it locally and open it in your browser:
|
|||||||
python3 -m http.server 9994 --directory examples/basics
|
python3 -m http.server 9994 --directory examples/basics
|
||||||
```
|
```
|
||||||
|
|
||||||
Go to `http://localhost:9994/hello.html`.
|
| URL | What it does |
|
||||||
|
|-----|-------------|
|
||||||
|
| `http://localhost:9994/hello.html` | Alerts "Hei maailma!" on load |
|
||||||
|
| `http://localhost:9994/factorial.html` | Prompts for n, displays n! |
|
||||||
|
| `http://localhost:9994/fibonacci.html` | Prompts for n, displays fib(n) |
|
||||||
|
|
||||||
## What is Tampio?
|
## What is Tampio?
|
||||||
|
|
||||||
Tampio programs read like Finnish sentences. Methods are defined using grammatical cases, so the language's syntax is shaped by natural-language morphology rather than punctuation.
|
Tampio programs read like Finnish sentences. Methods are defined using grammatical cases, so the language's syntax is shaped by natural-language morphology rather than punctuation.
|
||||||
|
|
||||||
```tampio
|
```tampio
|
||||||
[Tampio example will go here]
|
Pienen luvun kertoma on
|
||||||
|
riippuen siitä, onko se pienempi tai yhtä suuri kuin yksi,
|
||||||
|
joko yksi
|
||||||
|
tai pieni luku kerrottuna pienen luvun edeltäjän kertomalla.
|
||||||
```
|
```
|
||||||
|
|
||||||
|
`pienen luvun kertoma` — "the factorial of a small number". The parameter `pieni luku` (small number) appears in genitive case before the method name, and the recursive call `pienen luvun edeltäjän kertomalla` reads as "by the factorial of the predecessor of the small number".
|
||||||
|
|
||||||
See [`docs/cheatsheet.md`](docs/cheatsheet.md) for a map of Finnish grammatical cases to their roles in Tampio code, and [`docs/vs-javascript.md`](docs/vs-javascript.md) for side-by-side comparisons with JavaScript.
|
See [`docs/cheatsheet.md`](docs/cheatsheet.md) for a map of Finnish grammatical cases to their roles in Tampio code, and [`docs/vs-javascript.md`](docs/vs-javascript.md) for side-by-side comparisons with JavaScript.
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|||||||
@@ -0,0 +1,30 @@
|
|||||||
|
# Kertoma — Factorial in Tampio
|
||||||
|
#
|
||||||
|
# Methods are defined as: "[param in genitive] [method name] on [expression]"
|
||||||
|
# The parameter "pieni luku" (small number) appears in genitive: "pienen luvun"
|
||||||
|
#
|
||||||
|
# "riippuen siitä, onko ... joko ... tai ..." = ternary expression
|
||||||
|
# "pienempi tai yhtä suuri kuin yksi" = "less than or equal to one"
|
||||||
|
# "kerrottuna ... kertomalla" = "multiplied by [factorial]"
|
||||||
|
# Multiplication uses adessive case: "kertomalla" = "by the factorial"
|
||||||
|
|
||||||
|
Pienen luvun kertoma on
|
||||||
|
riippuen siitä, onko se pienempi tai yhtä suuri kuin yksi,
|
||||||
|
joko yksi
|
||||||
|
tai pieni luku kerrottuna pienen luvun edeltäjän kertomalla.
|
||||||
|
|
||||||
|
# "edeltäjä" = "predecessor" — defines n-1 as a helper method
|
||||||
|
|
||||||
|
Luvun edeltäjä on se vähennettynä yhdellä.
|
||||||
|
|
||||||
|
# "Olkoon X uusi muuttuja, jonka arvo on Y" = declare variable X with value Y
|
||||||
|
|
||||||
|
Olkoon pieni muuttuja uusi muuttuja, jonka arvo on nolla.
|
||||||
|
|
||||||
|
# Page-load event: prompt for a number, display its factorial
|
||||||
|
# "luetaan luku" = "a number is read" (browser prompt)
|
||||||
|
# "näyttää ... kertoman" = "shows the factorial of..." (genitive: kertoman)
|
||||||
|
|
||||||
|
Kun nykyinen sivu avautuu,
|
||||||
|
pieneen muuttujaan luetaan luku
|
||||||
|
ja nykyinen sivu näyttää pienen muuttujan arvon kertoman.
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
# Fibonacci-kani — Fibonacci Numbers in Tampio
|
||||||
|
#
|
||||||
|
# The method is named "kani" (rabbit) — a nod to Fibonacci's original rabbit problem,
|
||||||
|
# which is how he introduced the sequence in 1202.
|
||||||
|
#
|
||||||
|
# Base case: for n ≤ 1, fib(n) = n (covers fib(0)=0 and fib(1)=1 in one branch)
|
||||||
|
# Recursive: fib(n) = fib(n-1) + fib(n-2)
|
||||||
|
#
|
||||||
|
# fib(n-2) is computed by chaining: "edeltäjän edeltäjä" = predecessor of predecessor
|
||||||
|
# This avoids needing a two-word helper method name.
|
||||||
|
#
|
||||||
|
# "lisättynä ... kaniin" = "added to [kani value]" (illative case)
|
||||||
|
|
||||||
|
Pienen luvun kani on
|
||||||
|
riippuen siitä, onko se pienempi tai yhtä suuri kuin yksi,
|
||||||
|
joko pieni luku
|
||||||
|
tai pienen luvun edeltäjän kani lisättynä pienen luvun edeltäjän edeltäjän kaniin.
|
||||||
|
|
||||||
|
# "edeltäjä" = predecessor (n-1) — already in the standard library,
|
||||||
|
# redefined here for clarity.
|
||||||
|
|
||||||
|
Luvun edeltäjä on se vähennettynä yhdellä.
|
||||||
|
|
||||||
|
Olkoon pieni muuttuja uusi muuttuja, jonka arvo on nolla.
|
||||||
|
|
||||||
|
# "näyttää ... kanin" = "shows the rabbit/fibonacci of..." (genitive: kanin)
|
||||||
|
|
||||||
|
Kun nykyinen sivu avautuu,
|
||||||
|
pieneen muuttujaan luetaan luku
|
||||||
|
ja nykyinen sivu näyttää pienen muuttujan arvon kanin.
|
||||||
Reference in New Issue
Block a user