Skip to content

Instantly share code, notes, and snippets.

@wilbowma
Last active February 1, 2023 03:11
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wilbowma/94e935d7276ba2b7ef5ba2bc28daf447 to your computer and use it in GitHub Desktop.
Save wilbowma/94e935d7276ba2b7ef5ba2bc28daf447 to your computer and use it in GitHub Desktop.
A library for set!-ing numbers. You shouldn't use this.
#lang racket/base
(require
(for-syntax
racket/base
syntax/parse)
(rename-in
racket/base
[#%datum old-datum]
[set! old-set!]))
(define number-map (make-hasheq))
(define-syntax (set! stx)
(syntax-parse stx
[(_ i:number v)
#'(hash-set! number-map i v)]
[(_ . stx) #'(old-set! . stx)]))
(define-syntax (#%datum stx)
(syntax-parse stx
[(_ . i:number)
#'(hash-ref! number-map (old-datum . i) (old-datum . i))]
[stx #'(old-datum . stx)]))
(module+ test
(require rackunit)
(check-equal? 5 5)
(set! 5 6)
(check-equal? 5 6))
@greghendershott
Copy link

This is excellent!

I feel it would be even better if numbers could --- seeing as how sequence? already turns true -- behave as a sequence of digits. Perhaps octal digits?

A few more features like this, and I believe Racket could really hold its own in the stand-up WAT talk competition.

@wilbowma
Copy link
Author

Seems reasonable; added. Not sure how to get for to use this new interpretation of numbers as sequence by default, though...

@wilbowma
Copy link
Author

Should probably add support for reals by making that sequence lazy.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment