diff --git a/collects/tests/unstable/2d/cond-test.rkt b/collects/tests/unstable/2d/cond-test.rkt index 7fbac7f91c..67d2d58ee8 100644 --- a/collects/tests/unstable/2d/cond-test.rkt +++ b/collects/tests/unstable/2d/cond-test.rkt @@ -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) + + + + diff --git a/collects/unstable/2d/cond.rkt b/collects/unstable/2d/cond.rkt index 205af00cda..f0365eb01c 100644 --- a/collects/unstable/2d/cond.rkt +++ b/collects/unstable/2d/cond.rkt @@ -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)) diff --git a/collects/unstable/scribblings/2d.scrbl b/collects/unstable/scribblings/2d.scrbl index 7d48d6e09f..5c9b5b4604 100644 --- a/collects/unstable/scribblings/2d.scrbl +++ b/collects/unstable/scribblings/2d.scrbl @@ -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