docs/forthlisp-cheatsheet.txt (2850 bytes)
1 CHIP SCHEME CHEAT SHEET (R7RS-small subset, written in Forth, emulated STM32F446) 2 ================================================================================== 3 Start: stm32-lisp (boots QEMU, loads the Scheme, gives the > prompt) 4 Layers: assembly -> SBCL breadboard -> our own Forth kernel -> this Scheme. 5 The Scheme is Forth source loaded through the chip's Forth console. 6 Edit: Backspace and Delete both erase. An expression may span lines. 7 8 SWITCHING 9 (forth) leave Scheme, back to the Forth ok. prompt 10 lisp (at the Forth prompt) re-enter Scheme 11 Ctrl-C quit QEMU 12 13 DATA 14 42 -7 1/3 2.5 exact integers (31 bits; overflow is an error), rationals, 15 single-precision floats 16 #t #f booleans; only #f is false ('() is true) 17 #\a "a b\n" characters (bytes), strings 18 #(1 2 3) #u8(1 2) vectors, bytevectors 19 'a '(1 2 . 3) symbols, lists, dotted pairs 20 21 SPECIAL FORMS AND MACROS 22 (define x 10) (define (f a . rest) ...) (set! x 11) 23 (lambda (x) ...) (if c a b) (cond ...) (case ...) (and ...) (or ...) 24 (let ...) (let* ...) (letrec ...) (do ...) (when ...) (unless ...) 25 (define-syntax m (syntax-rules () ((_ x) ...))) not hygienic 26 `(a ,b ,@c) (delay x) (force p) (guard (e (#t ...)) body) 27 (parameterize ((p v)) ...) (define-record-type ...) (case-lambda ...) 28 (let-values (((q r) (floor/ 7 2))) ...) (call/cc ...) escape-only 29 30 EXAMPLES TO TYPE (each one is checked on the emulated chip) 31 (define (fact n) (if (< n 2) 1 (* n (fact (- n 1))))) 32 (fact 10) => 3628800 33 (map + '(1 2 3) '(10 20 30)) => (11 22 33) 34 (/ 6 4) => 3/2 35 (+ 1/2 0.25) => 0.75 36 (sqrt 16) => 4 37 (round 7/2) => 4 38 (string-append "ab" "cd") => "abcd" 39 (vector-ref #(1 2 3) 2) => 3 40 (char-upcase #\a) => #\A 41 (let ((x 5)) `(x ,x ,@(list 1 2))) => (x 5 1 2) 42 (call/cc (lambda (k) (+ 1 (k 42)))) => 42 43 (guard (e ((string? e) e)) (raise "oops")) => "oops" 44 (guard (e (#t (error-object-message e))) (car 5)) => "not a pair" 45 (let-values (((q r) (floor/ 17 5))) (list q r)) => (3 2) 46 (force (delay (* 6 7))) => 42 47 (let loop ((i 0)) (if (< i 10000) (loop (+ i 1)) i)) => 10000 48 (+ 1073741823 1) => overflow 49 50 LIMITS 51 60 KiB cons heap, 12 KiB string/vector heap, 10 KiB symbol names 52 tail calls run in constant stack; deep non-tail recursion: too deep 53 continuations are escape-only; syntax-rules is not hygienic 54 Errors print a message and return to > unless a handler catches them