boot2

Playing with the boostrap
git clone https://git.ryansepassi.com/git/boot2.git
Log | Files | Refs | README

082-cond-arrow.scm (1789B)


      1 ; (cond (test => proc) ...) -- if test is truthy, call proc on the
      2 ; value of test and return that. proc is any expression that evaluates
      3 ; to a 1-arg procedure (a name, a lambda, etc.).
      4 
      5 ;; --- Basic: => with a named procedure ----------------------------------
      6 ;; assoc returns the binding pair when the key is found, #f otherwise.
      7 ;; (assoc 'b ...) -> (b . 2); (cdr ...) -> 2.
      8 (if (= 2
      9        (cond ((assoc 'b (list (cons 'a 1) (cons 'b 2) (cons 'c 3))) => cdr)
     10              (else 99)))
     11     0 (sys-exit 1))
     12 
     13 ;; --- Falsy test: => clause skipped, falls through to else ------------
     14 (if (= 99
     15        (cond ((assoc 'z (list (cons 'a 1))) => cdr)
     16              (else 99)))
     17     0 (sys-exit 2))
     18 
     19 ;; --- proc as a lambda expression ---------------------------------------
     20 (if (= 84
     21        (cond (42 => (lambda (v) (+ v v)))
     22              (else 0)))
     23     0 (sys-exit 3))
     24 
     25 ;; --- proc as the result of evaluating a non-trivial expression --------
     26 ;; Confirms proc-expr is eval'd in env, not just looked up by symbol.
     27 (define (mk-doubler) (lambda (x) (* x 2)))
     28 (if (= 14
     29        (cond (7 => (mk-doubler))
     30              (else 0)))
     31     0 (sys-exit 4))
     32 
     33 ;; --- Mixed clauses: => after a regular truthy clause never fires -----
     34 (if (= 1
     35        (cond (#t 1)
     36              (#t => (lambda (v) (sys-exit 5)))))
     37     0 (sys-exit 6))
     38 
     39 ;; --- Mixed clauses: => after a falsy regular clause does fire --------
     40 (if (= 5
     41        (cond (#f 99)
     42              (5 => (lambda (v) v))
     43              (else 0)))
     44     0 (sys-exit 7))
     45 
     46 ;; --- => binds the value of test, not just truthiness ------------------
     47 ;; The test value (the pair) is what gets passed to cdr, not #t.
     48 (if (equal? '(b . 2)
     49             (cond ((assoc 'b (list (cons 'a 1) (cons 'b 2))) =>
     50                    (lambda (p) p))))
     51     0 (sys-exit 8))
     52 
     53 (sys-exit 0)