forked from grol-io/grol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample.gr
36 lines (29 loc) · 1.16 KB
/
sample.gr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
Sample file that our gorepl can interpret
This is a block commen
See also the other *.gr files
*/
unless = macro(cond, iffalse, iftrue) {
quote(if (!(unquote(cond))) {
unquote(iffalse)
} else {
unquote(iftrue)
})
}
unless(10 > 5, print("BUG: not greater\n"), print("macro test: greater\n"))
// first class function objects, can also be written as `func fact(n) {` as shorthand
// or fact = (n => ...) as a lambda short form.
fact=func(n) {
log("called fact ", n) // log (timestamped stderr output)
if (n<=1) {
return 1
}
/* recursion: */ n*self(n-1) // also last evaluated expression is returned (ie return at the end is optional)
}
a=[fact(5), "abc", 76-3, sqrt(2)] // array can contain different types, grol also has math functions.
m={"key": a, 73: 29} // so do maps
println("m is:", m) // stdout print
println("Outputting a smiley: 😀")
first(m.key) // get the value from key from map, which is an array, and the first element of the array is our factorial 5
// (dot notation .key is shorthand for ["key"]), could also have been first(m["key"]) or m["key"][0] or m.key[0]
// ^^^ gorepl sample.gr should output 120