Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Arithmetic

Three operators, all binary, all on 64-bit signed integers:

(+ 2 3)
;=> 5
(- 2 3)
;=> -1
(* 2 3)
;=> 6

There is no division, no modulo, no comparison other than eq?, and no numeric tower — the reference base.scm has exactly these three, and narju follows it. What the set lacks in convenience it recovers in composability with the boolean encoding:

(* (number? 3) (null? nil))
;=> 1

* is conjunction, + (on 0/1 values) is close to disjunction, and (if (- a b) 0 1) is numeric equality, read as “false iff the difference is nonzero”. Ordering comparisons, where needed, are written recursively — count both numbers down together and see which reaches zero first:

(let less?
  (lambda less? a
    (lambda _ b
      (if (eq? b 0) 0
        (if (eq? a 0) 1
          ((less? (- a 1)) (- b 1))))))
  (less? 3 5))
;=> 1

Operators are special forms, not values — + cannot be passed to a function. Where a library needs a first-class operation it wraps one:

(let add (lambda _ a (lambda _ b (+ a b)))
  (add 20 22))
;=> 42

The operands must both be numbers (or code, staging the operation — Part II):

(+ 1 'a)
;! type error in +: expected matching Cst or Code, got (Cst(1), Sym("a"))