add 'else' support to 2dcond
closes PR 13714
This commit is contained in:
parent
9d43fcad3e
commit
4bef2987a9
|
@ -81,3 +81,24 @@
|
|||
"1\n2\n3\n4\n"))
|
||||
|
||||
|
||||
(define (try-else a b c)
|
||||
#2dcond
|
||||
╔════╦════╦════╗
|
||||
║ ║ a ║else║
|
||||
╠════╬════╬════╣
|
||||
║ b ║ 1 ║ 2 ║
|
||||
╠════╬════╬════╣
|
||||
║ c ║ 3 ║ 4 ║
|
||||
╠════╬════╬════╣
|
||||
║else║ 5 ║ 6 ║
|
||||
╚════╩════╩════╝)
|
||||
|
||||
(require rackunit)
|
||||
(check-equal? (try-else #t #t #f) 1)
|
||||
(check-equal? (try-else #f #t #f) 2)
|
||||
(check-equal? (try-else #t #f #f) 5)
|
||||
(check-equal? (try-else #f #f #f) 6)
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#lang racket
|
||||
#lang racket/base
|
||||
(require (for-syntax racket/base))
|
||||
|
||||
(provide 2dcond)
|
||||
(define-syntax (2dcond stx)
|
||||
|
@ -7,6 +8,9 @@
|
|||
[(cell ...) rhs ...] ...)
|
||||
(let ()
|
||||
|
||||
(define last-col (- (length (syntax->list #'widths)) 1))
|
||||
(define last-row (- (length (syntax->list #'heights)) 1))
|
||||
|
||||
;; coord-to-content : hash[(list num num) -o> (listof syntax)]
|
||||
(define coord-to-content (make-hash))
|
||||
(define let-bindings '())
|
||||
|
@ -35,6 +39,16 @@
|
|||
|
||||
(cond
|
||||
[(member (list 0 0) cells) (void)]
|
||||
[(and (or (member (list 0 last-row) cells)
|
||||
(member (list last-col 0) cells))
|
||||
(syntax-case rhses (else)
|
||||
[(else) #t]
|
||||
[_ #f]))
|
||||
;; found an 'else' (in a reasonable place)
|
||||
;; => treat it like a #t in that cell
|
||||
(hash-set! coord-to-content
|
||||
(car cells)
|
||||
(list #'#t))]
|
||||
[(and
|
||||
;; only one cell:
|
||||
(null? (cdr cells))
|
||||
|
|
|
@ -90,9 +90,15 @@ the same.
|
|||
@defform/subs[(2dcond cond-content)
|
||||
([cond-content (code:line question-row
|
||||
body-row
|
||||
⋮)]
|
||||
[question-row (code:line empty-cell question-cell ⋯)]
|
||||
⋮)
|
||||
(code:line question-row
|
||||
body-row
|
||||
⋮
|
||||
else-row)]
|
||||
[question-row (code:line empty-cell question-cell ⋯)
|
||||
(code:line empty-cell question-cell ⋯ else-cell)]
|
||||
[body-row (code:line question-cell exprs-cell ⋯)]
|
||||
[else-row (code:line question-cell exprs-cell ⋯ else-cell)]
|
||||
[question-cell (code:line ╔═════════════╗
|
||||
║question-expr║
|
||||
╚═════════════╝)]
|
||||
|
@ -103,7 +109,10 @@ the same.
|
|||
|
||||
[exprs-cell (code:line ╔═════════════╗
|
||||
║expr expr ...║
|
||||
╚═════════════╝)])]{
|
||||
╚═════════════╝)]
|
||||
[else-cell (code:line ╔══════╗
|
||||
║ else ║
|
||||
╚══════╝)])]{
|
||||
Evaluates the first row of question expressions until
|
||||
one of them returns a true value (signaling an error if none do),
|
||||
then evaluates the first column of question expressions until
|
||||
|
|
Loading…
Reference in New Issue
Block a user