(Arne Babenhauserheide)
2015-08-04: add a closure example add a closure example
diff --git a/examples/closure.w b/examples/closure.w new file mode 100644 --- /dev/null +++ b/examples/closure.w @@ -0,0 +1,26 @@ +#!/usr/bin/env sh +# -*- wisp -*- +exec guile -L $(dirname $(dirname $(realpath "$0"))) --language=wisp -e '(@@ (examples closure) main)' -s "$0" "$@" +; !# + +;; A simple example for a closure + + +define counting-closure ; simple variable + let : : counter 0 ; provide counter to hold local data + lambda () ; the variable is bound to a function -> callable + set! counter : 1+ counter ; adjust the counter shared by all function calls + . counter + + +; counter is created outside the function definition (lambda), so the +; change survives over function calls. It is function-local data. + + +define : main args + display : counting-closure + newline ; 1 + display : counting-closure + newline ; 2 + display : counting-closure + newline ; 3