Word wrap: Difference between revisions

784 bytes added ,  11 years ago
mNo edit summary
Line 943:
 
=={{header|Racket}}==
<lang racket>
#lang racket
 
(define (wrap words width)
(define (maybe-cons xs xss)
(if (empty? xs) xss (cons xs xss)))
(match/values
(for/fold ([lines '()] [line '()] [left width]) ([w words])
(define n (string-length w))
(cond
[(> n width) ; word longer than line => line on its own
(values (cons (list w) (maybe-cons line lines)) '() width)]
[(> n left) ; not enough space left => new line
(values (cons line lines) (list w) (- width n 1))]
[else
(values lines (cons w line) (- left n 1))]))
[(lines line _)
(apply string-append
(for/list ([line (reverse (cons line lines))])
(string-join line #:after-last "\n")))]))
 
;;; Usage:
(wrap (string-split text) 70)
</lang>
 
=={{header|REXX}}==
The input for this program is in a file (named LAWS.TXT).
Anonymous user