Skip to main content

Functions

Functions in HIL LISP are defined using DEFUN and support parameters, recursion, and lexical scoping. Functions are the primary mechanism for code reuse and abstraction (Abelson et al., 1996).

Defining Functions​

(DEFUN name (param1 param2 ...) body)

Examples:

; Simple function
(DEFUN DOUBLE (x) (* x 2))
(DOUBLE 5) ; 10

; Multiple parameters
(DEFUN ADD (a b) (+ a b))
(ADD 3 4) ; 7

; Function with no parameters
(DEFUN NOW () (MILLIS))

Recursion​

Functions can call themselves. HIL LISP supports recursive function definitions following the principles established by McCarthy (1960):

; Factorial
(DEFUN FACT (n)
(IF (<= n 1) 1 (* n (FACT (- n 1)))))

(FACT 5) ; 120
(FACT 10) ; 3628800

Fibonacci Sequence:

(DEFUN FIB (n)
(IF (<= n 2) n
(+ (FIB (- n 1)) (FIB (- n 2)))))

(FOR i 1 11
(LOG "fib(" i ") = " (FIB i)))

Scope​

HIL LISP uses dynamic scoping for function parameters. When a function is called, its parameters are temporarily bound in the global environment, and restored after the call returns:

(SET x 100)

(DEFUN GET-X () x)

(GET-X) ; 100

; After function call, x is still 100

When a function with a parameter x is called, the parameter value temporarily replaces the global x, then the original value is restored:

(SET x 10)
(DEFUN USE-X (x) (LOG "x inside: " x))
(USE-X 42) ; logs "x inside: 42"
x ; still 10

LAMBDA — Anonymous Functions & Closures​

LAMBDA creates a function without a name that you can store in a variable, pass as an argument, or return from another function. Since HIL 12.0, lambdas are true first-class values with lexical closures.

In Plain Terms (No Programming Background Needed)​

Think of a regular function (DEFUN) as an appliance bolted to your kitchen wall — useful, but fixed in place. A lambda is like a small hand-held gadget:

  • It has no name — you just build it and hand it over.
  • It fits in your pocket — you can put it in a box (a variable), mail it to a friend (pass it to another function), or have a factory produce thousands of them.
  • It remembers where it came from — this is the magic part. If you build the gadget in a room where the thermostat reads 25°, the gadget remembers 25° even after you carry it into another room. That memory is called a closure.

Real-world analogy: a prepaid transit card. The card itself is simple — tap and go. But it remembers how much money was loaded when it was created. Two cards made at different times behave differently because each remembers its own balance:

(SET make-card (LAMBDA (balance) ; factory: load a card with `balance`
(LAMBDA () ; the card itself: tap = spend one credit
(DO
(SET balance (- balance 1)) ; remember what's left
balance))))

(SET card-a (make-card 10)) ; a card loaded with 10 rides
(SET card-b (make-card 2)) ; a card loaded with 2 rides

(card-a) ; → 9 (card-a still remembers ITS balance)
(card-a) ; → 8
(card-b) ; → 1 (separate memory — card-b is untouched by card-a)

Each call to make-card produces an independent function with its own private memory. Nothing global changed; no other variable can reach in and tamper with the balance.

For Developers​

LAMBDA evaluates to a closure value — a triple (args, body, captured-env):

(LAMBDA (param1 param2 ...) body...)
  • First-class: closures are ordinary values. Bind them with SET, pass them as arguments, return them from functions.
  • Lexical scoping: the closure captures its defining environment, not the caller's. Free variables resolve where the lambda was written, following SICP/McCarthy semantics (Abelson et al., 1996).
  • Multi-form bodies: body may contain several forms; the last form's value is returned.
  • Recursion-safe: a bound name resolves to the closure itself on subsequent calls.
;; Named lambda — equivalent power to DEFUN, stored in a variable
(SET square (LAMBDA (x) (* x x)))
(square 5) ; → 25

;; Immediately-invoked lambda expression (IIFE)
((LAMBDA (a b) (+ (* a a) (* b b))) 3 4) ; → 25

;; Function factory returning a closure
(SET make-adder (LAMBDA (n) (LAMBDA (x) (+ x n))))
(SET add5 (make-adder 5))
(add5 10) ; → 15 (add5's environment keeps n=5 alive)

Closures Capture State​

A closure keeps its captured variables alive between calls — enabling private, persistent state without globals:

(DEFUN MAKE-COUNTER ()
(DO
(SET n 0)
(LAMBDA () (DO (SET n (+ n 1)) n))))

(SET tick (MAKE-COUNTER))
(tick) ; 1
(tick) ; 2
(tick) ; 3 ← n survives inside tick's closed-over environment

Higher-Order Functions​

Functions that take or return functions are fully supported:

;; Lambda passed INTO a DEFUN
(DEFUN APPLY-TWICE (f v) (DO (f v) (f v)))
(APPLY-TWICE (LAMBDA (x) (* x 2)) 7)

;; Lambda passed INTO another lambda
(SET compose-call (LAMBDA (g x) (g x)))
(compose-call (LAMBDA (x) (+ x 100)) 5) ; → 105

;; Callback pattern for hardware events
(ON-EVENT 2 FALLING
(LAMBDA () (WRITE D 13 1)))

Where Lambdas Work​

TargetSupportNotes
Interpreter / simulate✅ FullClosures, state, higher-order — everything above
Node.js / Python / Go ports✅ FullSame semantics
Transpiled C (.hex, CRUMB)⚠️ PartialTop-level (SET name (LAMBDA ...)) becomes a static C function. Runtime closures and capturing lambdas cannot exist in C and are emitted with a warning

Tip: For firmware-bound scripts, keep top-level named lambdas (they compile to efficient static functions) and reserve runtime closures for simulation and PC-side logic.

Legacy Dispatch Pattern (Still Works)​

Before lambdas existed, higher-order behavior required string-based dispatch. This remains valid — but prefer real lambdas for new code:

(DEFUN dispatch (mode)
(COND
((== mode 0) (handler-led))
((== mode 1) (handler-servo))))

Practical Examples​

Temperature Average​

(DEFUN READ-TEMP (pin samples)
(DO
(SET sum 0)
(FOR i 0 samples
(SET sum (+ sum (MAP (READ A pin) 0 1023 0 100)))
(DELAY 10))
(/ sum samples)))

(SET temp (READ-TEMP 0 10))
(LOG "Average temperature: " temp "C")

Debounced Button Read​

(DEFUN DEBOUNCE (pin)
(DO
(SET v1 (READ D pin))
(DELAY 50)
(SET v2 (READ D pin))
(IF (== v1 v2) v1 0)))

; Usage
(WHILE 1
(IF (== (DEBOUNCE 2) 1)
(LOG "Button pressed!")))

Moving Average Filter​

(SET idx 0)
(SET buf-size 5)
(SET buf0 0)(SET buf1 0)(SET buf2 0)(SET buf3 0)(SET buf4 0)

(DEFUN SET-BUF (i v)
(COND
((== i 0) (SET buf0 v))
((== i 1) (SET buf1 v))
((== i 2) (SET buf2 v))
((== i 3) (SET buf3 v))
((== i 4) (SET buf4 v))))

(DEFUN GET-BUF (i)
(COND
((== i 0) buf0)
((== i 1) buf1)
((== i 2) buf2)
((== i 3) buf3)
((== i 4) buf4)))

(DEFUN MOVING-AVG (new-val)
(DO
(SET-BUF idx new-val)
(SET idx (% (+ idx 1) buf-size))
(SET sum 0)
(FOR i 0 buf-size (SET sum (+ sum (GET-BUF i))))
(/ sum buf-size)))

References

Abelson, H., Sussman, G. J., & Sussman, J. (1996). Structure and interpretation of computer programs (2nd ed.). MIT Press.

McCarthy, J. (1960). Recursive functions of symbolic expressions and their computation by machine, Part I. Communications of the ACM, 3(4), 184–195.