A program
Everything so far, in one file.
The shape of a file
What goes in a file, and in what order?
Definitions first, then the lines that use them. Koskari runs top to bottom, so a function has to be defined before the line that calls it.
Where do the “do it now” lines go?
In one function marked @entry-point. It runs after the whole file has
loaded, when the file is run directly with koskari, and not when
another file borrows its definitions:
def greet [name]:
print f"hello, {name}"
@entry-point
def main []:
(greet "Ada")
The @ line sits over the def and marks it. Any name works;
main is the custom.
Borrowing from Python
Does Koskari have a square root?
Python does, and Koskari can use anything Python has. import brings a
module in, and a dot reaches into it:
import math
print (math.sqrt 16)
That prints 4.0.
Can we bring in just one thing?
import-from names the module and then the things you want:
import-from math sqrt pi
print (sqrt 16) pi
The program
Put it together.
Save this as report.ki and run koskari report.ki:
import-from statistics mean
var scores #[72 88 95 61 88]
def average [numbers]:
(round (mean numbers) 1)
def grade [score]:
if (>= score 90):
"A"
elif (>= score 80):
"B"
elif (>= score 70):
"C"
else:
"D"
def report [numbers]:
for-each [score numbers]:
print f"{score}: {(grade score)}"
print f"average {(average numbers)}, best {(max numbers)}"
@entry-point
def main []:
(report scores)
What does it print?
72: C
88: B
95: A
61: D
88: B
average 80.8, best 95
Five lines from the loop, then one from the last print.
What in it is new?
Only statistics.mean, borrowed from Python, and round, which trims a
number to one decimal place. Everything else is a list, a function, a
choice, a loop, and text, from the chapters before.
What would you change first?
The scores. Then the grade boundaries. Then add a line to report that
counts how many scores are above the average, which needs a var
before the loop and an if inside it.