5 ms·
> The SLED system does not feature numeric types. Yet natural numbers (non-negative integers) can be emulated using lists: > '(nil nil nil) ; three Pfsh, how
by anyfoo 6d ago
> The SLED system does not feature numeric types. Yet natural numbers (non-negative integers) can be emulated using lists:
> '(nil nil nil) ; three
Pfsh, how pedestrian and unnecessarily high level. What you of course want to use instead is ♫ Church Numerals ♫!
(define zero (lambda (f) (lambda (x) x)))
(define (succ n) (lambda (f) (lambda (x) (f ((n f) x)))))
(define one (succ zero))
(define two (succ one))
(define (add m n) (lambda (f) (lambda (x) ((m f) ((n f) x)))))
(define (mul m n) (lambda (f) (n (m f))))
(define (pow m n) (n m)) ; n applications of "multiply by m"
;; escape hatch back to the host numbers
(define (church->int n) ((n (lambda (x) (+ x 1))) 0))
(The "escape hatch" at the end is in Scheme, and only shown for illustration. Converting it into SLED and its documented "list numbers" is left as an exercise for the reader. It's really trivial, try it.)