Word wrap: Difference between revisions

Content added Content deleted
(Nimrod -> Nim)
(→‎{{header|LFE}}: Tightened up code a bit.)
Line 1,353: Line 1,353:
(defun make-wrapped-lines
(defun make-wrapped-lines
(((cons word rest) max-len)
(((cons word rest) max-len)
(let (((tuple _ len last-line lines) (assemble-lines
(let ((`#(,_ ,_ ,last-line ,lines) (assemble-lines max-len word rest)))
max-len
word
rest)))
(lists:reverse (cons last-line lines)))))
(lists:reverse (cons last-line lines)))))


Line 1,362: Line 1,359:
(lists:foldl
(lists:foldl
#'assemble-line/2
#'assemble-line/2
(tuple max-len (length word) word '()) rest))
`#(,max-len ,(length word) ,word ())
rest))


(defun assemble-line
(defun assemble-line
((word (tuple max line-len line acc))
((word `#(,max ,line-len ,line ,acc)) (when (> (+ (length word) line-len) max))
(when (> (+ (length word) line-len) max))
`#(,max ,(length word) ,word ,(cons line acc)))
(tuple max (length word) word (cons line acc)))
((word `#(,max ,line-len ,line ,acc))
((word (tuple max line-len line acc))
`#(,max ,(+ line-len 1 (length word)) ,(++ line " " word) ,acc)))
(tuple max (+ line-len 1 (length word)) (++ line " " word) acc)))
</lang>
</lang>


Line 1,375: Line 1,372:


<lang Lisp>
<lang Lisp>
> (set test-text "Even today, with proportional fonts and complex layouts, there are still cases where you need to wrap text at a specified column. The basic task is to wrap a paragraph of text in a simple way in your language. If there is a way to do this that is built-in, trivial, or provided in a standard library, show that. Otherwise implement the minimum length greedy algorithm from Wikipedia.")
> (set test-text (++ "Even today, with proportional fonts and complex layouts, there are still cases where you need to wrap text at a specified column. "
"The basic task is to wrap a paragraph of text in a simple way in your language. If there is a way to do this that is built-in, trivial, or "
"provided in a standard library, show that. Otherwise implement the minimum length greedy algorithm from Wikipedia.")
> (io:format "~s~n" (list (wrap-text test-text)))
> (io:format "~s~n" (list (wrap-text test-text)))
</lang>
<pre>
Even today, with proportional fonts and complex layouts, there are still cases
Even today, with proportional fonts and complex layouts, there are still cases
where you need to wrap text at a specified column. The basic task is to wrap a
where you need to wrap text at a specified column. The basic task is to wrap a
Line 1,383: Line 1,384:
Otherwise implement the minimum length greedy algorithm from Wikipedia.
Otherwise implement the minimum length greedy algorithm from Wikipedia.
ok
ok
</pre>
<lang lisp>
> (io:format "~s~n" (list (wrap-text test-text 50)))
> (io:format "~s~n" (list (wrap-text test-text 50)))
</lang>
<pre>
Even today, with proportional fonts and complex
Even today, with proportional fonts and complex
layouts, there are still cases where you need to
layouts, there are still cases where you need to
Line 1,393: Line 1,398:
length greedy algorithm from Wikipedia.
length greedy algorithm from Wikipedia.
ok
ok
</pre>
>
</lang>


=={{header|Lua}}==
=={{header|Lua}}==