Skip to content

Instantly share code, notes, and snippets.

@gambiteer
Created April 19, 2025 18:30
Show Gist options
  • Save gambiteer/6a3db87763f54934953130aa94db929a to your computer and use it in GitHub Desktop.
Save gambiteer/6a3db87763f54934953130aa94db929a to your computer and use it in GitHub Desktop.
Conway's Game of Life in Gambit Scheme with SRFI 231
(declare
(standard-bindings)
(extended-bindings)
(block)
(not inline) ;; inlining distorts what I want to measure
(safe) ;; default declaration, made explicit
)
(import (srfi 231))
;;; for chibi:
#;(import (chibi time) (srfi 143))
(define (array-pad-periodically a N)
;; Pad a periodically with N rows and columns top and bottom, left and right.
;; Assumes that the domain of a has zero lower bounds.
;; Returns a generalized array.
(let* ((domain (array-domain a))
(m (interval-upper-bound domain 0))
(n (interval-upper-bound domain 1))
(a_ (array-getter a)))
(make-array (interval-dilate domain (vector (- N) (- N)) (vector N N))
(lambda (i j)
(a_ (modulo i m) (modulo j n))))))
(define (neighbor-count a)
(let* ((big-a (array-copy (array-pad-periodically a 1)
(array-storage-class a)))
(domain (array-domain a))
(translates (map (lambda (translation)
(array-extract (array-translate big-a translation)
domain))
'(#(1 0) #(0 1) #(-1 0) #(0 -1)
#(1 1) #(1 -1) #(-1 1) #(-1 -1)))))
;; Returns a generalized array that contains the number
;; of 1s in the 8 cells surrounding each cell in the original array.
(apply array-map
(lambda (a b c d e f g h)
(fx+ a b c d e f g h))
translates)))
(define (game-rules a neighbor-count)
;; a is a single cell, neighbor-count is the count of 1s in
;; its 8 neighboring cells.
(if (= a 1)
(if (or (= neighbor-count 2)
(= neighbor-count 3))
1 0)
;; (= a 0)
(if (= neighbor-count 3)
1 0)))
(define (advance a)
;; Returns a specialized array
(array-copy
(array-map game-rules a (neighbor-count a))
(array-storage-class a)))
(define life-storage-class generic-storage-class)
(define glider
(list*->array
2
'((0 0 0 0 0 0 0 0 0 0)
(0 0 1 0 0 0 0 0 0 0)
(0 0 0 1 0 0 0 0 0 0)
(0 1 1 1 0 0 0 0 0 0)
(0 0 0 0 0 0 0 0 0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0 0 0 0 0)
)
life-storage-class
))
(define a
(time
(advance (advance (advance (advance (advance (advance (advance (advance (advance (advance
(advance (advance (advance (advance (advance (advance (advance (advance (advance (advance
(advance (advance (advance (advance (advance (advance (advance (advance (advance (advance
(advance (advance (advance (advance (advance (advance (advance (advance (advance (advance
(advance (advance (advance (advance (advance (advance (advance (advance (advance (advance
(advance (advance (advance (advance (advance (advance (advance (advance (advance (advance
(advance (advance (advance (advance (advance (advance (advance (advance (advance (advance
(advance (advance (advance (advance (advance (advance (advance (advance (advance (advance
(advance (advance (advance (advance (advance (advance (advance (advance (advance (advance
(advance (advance (advance (advance (advance (advance (advance (advance (advance (advance
glider-gun
))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))
(pretty-print (array->list* a))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment