From 37871b6b3b76f51a0c8d871aa655ca12b42e99db Mon Sep 17 00:00:00 2001 From: w6vvn Date: Mon, 1 Sep 2025 18:57:01 -0700 Subject: [PATCH] implement a procedure for reflowing and displaying paragraphs --- gem300.rkt | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/gem300.rkt b/gem300.rkt index b25f292..9f3028d 100644 --- a/gem300.rkt +++ b/gem300.rkt @@ -42,6 +42,34 @@ [else (values (string->number status) meta)]))))) +;; takes one long string and reflows it within an 80 character wide +;; column +(define (render-paragraph paragraph) + ;; collects from one list of words into another such that the new + ;; list does not exceed 80 characters when joined, and returns the + ;; new list and remainder of the first list + (define (inner-iter acc rst) + (let ([line (string-join acc)]) + (if (or (empty? rst) + (> (string-length line) 80)) + (values acc rst) + (inner-iter (append acc (list (car rst))) + (cdr rst))))) + + ;; collects from a list of words into sublists of words such that + ;; each sublist is no greater than 80 characters when joined + (define (outer-iter acc rst) + (if (empty? rst) + acc + (let-values ([(inner-acc inner-rst) + (inner-iter (list) rst)]) + (outer-iter (append acc (list inner-acc)) inner-rst)))) + + ;; join each sublist into one string, and display one string per + ;; line + (for-each displayln + (map string-join (outer-iter (list) (string-split paragraph))))) + (define commands (list (cons "default" (lambda (line) (void)))