fix product function and add examples

This commit is contained in:
Mark
2023-10-19 19:38:13 +02:00
parent 1cb84fea43
commit 42288494f0
7 changed files with 64 additions and 2 deletions

View File

@@ -0,0 +1 @@
"Hello, World!".println

View File

@@ -0,0 +1,3 @@
"What's you name?".println
name := ().read_line.trim
("Hello, ", name, "!").concat.println

13
examples/02_Calc_Sum.mers Normal file
View File

@@ -0,0 +1,13 @@
total := 0.0
{() -> {
("Total: ", total, ". Type a number to change.").concat.println
(
().read_line.trim.parse_float,
(
n -> &total = (total, n).sum,
// not a number, so return a 1-tuple to break from the loop
() -> (())
)
).try
}}.loop
"Goodbye.".println

View File

@@ -0,0 +1,30 @@
"- Calculator -".println
"Type =<num> to set the value to that number.".println
"Type +<num>, -<num> or *<num> to change the value.".println
"Type exit to exit.".println
current := 0.0
{() -> {
("[ ", current, " ]").concat.println
input := ().read_line.trim
num := (
(input, 1).substring.trim.parse_float,
(
() -> 0.0,
val -> val
)
).try
mode := (input, 0, 1).substring
if (mode, "+").eq {
&current = (current, num).sum
} else if (mode, "-").eq {
&current = (current, (num, -1).product).sum
} else if (mode, "*").eq {
&current = (current, num).product
} else if (mode, "=").eq {
&current = num
} else if (input, "exit").eq {
(())
}
}}.loop