A Lisp interpreter
Stage 7 of 11v2 · 2625f390

Lambdas

Create functions with parameters and a body, validate arity, and execute calls.

Lambdas

Create functions with parameters and a body, validate arity, and execute calls. This stage adds one observable capability to the project; keep every operation from earlier stages working.

Model and contract

The executable reads a Lisp expression or program from stdin and returns a canonical value representation. For these tests, a JSON request selects reader and evaluator operations.

Representative input:

{ "op": "eval", "source": "((lambda (x) (* x x)) 5)" }

Exact output:

{ "value": 25 }

The output ends with a newline. For an invalid request, leave stdout empty, write a diagnostic containing error to stderr, and exit with a non-zero status.

How to approach it

Keep input parsing, core logic, and output serialization separate. First write down the invariants behind “lambdas”, walk through the example by hand, and exercise boundaries before optimizing. Do not replace the mechanism taught by this stage with a library function that solves it completely.

Pay particular attention to empty inputs, index or length boundaries, and malformed data. The result must be deterministic: preserve the ordering required by the request and emit compact JSON.

Acceptance criteria

  • The representative input produces exactly the output shown.
  • Invalid input follows the stderr and exit-status contract.
  • Capabilities from earlier stages keep working.