implement a procedure for reflowing and displaying paragraphs

This commit is contained in:
2025-09-01 18:57:01 -07:00
parent e0260e4496
commit 37871b6b3b

View File

@@ -42,6 +42,34 @@
[else [else
(values (string->number status) meta)]))))) (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 (define commands
(list (list
(cons "default" (lambda (line) (void))) (cons "default" (lambda (line) (void)))