Created
March 31, 2021 18:57
Simple macro making a closure over shared state.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang racket/base | |
(require (for-syntax syntax/parse/lib/function-header) | |
syntax/parse/define | |
racket/match | |
racket/undefined) | |
#| | |
(define fun (let (state ....) (lambda (args) (set! state ...)))) | |
|# | |
(define-syntax-parse-rule | |
(define/local-state (name:id args:formals ...) (locals:id ...) body ...) | |
(define name | |
(let ([locals undefined] ...) | |
(lambda (args ...) body ...)))) | |
(define/local-state (doit msg) (a b c) | |
(match msg | |
['assign | |
(set!-values (a b c) (values 1 2 3))] | |
['report | |
(list a b c)])) | |
(doit 'report) | |
;; => '(#<undefined> #<undefined> #<undefined>) | |
(doit 'assign) | |
(doit 'report) | |
;; => '(1 2 3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment