Add for/max and for*/max stolen from Racket docs to library

This commit is contained in:
Jonathan Chan 2022-12-08 11:11:35 -05:00
parent cfb363930a
commit 06542efb47
2 changed files with 33 additions and 5 deletions

31
lib.rkt
View File

@ -7,12 +7,41 @@
enqueue!) enqueue!)
(only-in racket/set (only-in racket/set
list->set list->set
set->list)) set->list)
(for-syntax syntax/for-body)
syntax/parse/define)
(provide (all-from-out threading) (provide (all-from-out threading)
(all-defined-out)) (all-defined-out))
;; Macros ;;
(define-syntax-parse-rule (for/max clauses body ... tail-expr)
#:with original this-syntax
#:with ((pre-body ...) (post-body ...)) (split-for-body this-syntax #'(body ... tail-expr))
(for/fold/derived original
([current-max -inf.0])
clauses
pre-body ...
(define maybe-new-max (begin post-body ...))
(if (> maybe-new-max current-max)
maybe-new-max
current-max)))
(define-syntax-parse-rule (for*/max clauses body ... tail-expr)
#:with original this-syntax
#:with ((pre-body ...) (post-body ...)) (split-for-body this-syntax #'(body ... tail-expr))
(for*/fold/derived original
([current-max -inf.0])
clauses
pre-body ...
(define maybe-new-max (begin post-body ...))
(if (> maybe-new-max current-max)
maybe-new-max
current-max)))
;; Function helpers ;; ;; Function helpers ;;
(define compose) (define compose)

View File

@ -62,9 +62,8 @@
(- r r*)))) (- r r*))))
(define part2 (define part2
(maximum (for*/max ([r (in-range 1 (sub1 height))]
(for*/list ([r (in-range 1 (sub1 height))]
[c (in-range 1 (sub1 width))]) [c (in-range 1 (sub1 width))])
(score r c)))) (score r c)))
(show-solution (part1) part2) (show-solution (part1) part2)