implement a procedure for reflowing and displaying paragraphs
This commit is contained in:
28
gem300.rkt
28
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)))
|
||||
|
Reference in New Issue
Block a user