Word wrap: Difference between revisions

→‎{{header|LFE}}: Tightened up code a bit.
(Nimrod -> Nim)
(→‎{{header|LFE}}: Tightened up code a bit.)
Line 1,353:
(defun make-wrapped-lines
(((cons word rest) max-len)
(let ((`#(tuple,_ ,_ len ,last-line ,lines) (assemble-lines max-len word rest)))
max-len
word
rest)))
(lists:reverse (cons last-line lines)))))
 
Line 1,362 ⟶ 1,359:
(lists:foldl
#'assemble-line/2
`#(tuple ,max-len ,(length word) ,word '()) rest))
rest))
 
(defun assemble-line
((word `#(tuple ,max ,line-len ,line ,acc)) (when (> (+ (length word) line-len) max))
`#(when (> (+,max ,(length word) ,word ,(cons line-len) maxacc)))
(tuple max (length word) word`#(,max (cons,line-len ,line ,acc)))
((word (tuple `#(,max ,(+ line-len 1 (length word)) ,(++ line " " word) ,acc)))
(tuple max (+ line-len 1 (length word)) (++ line " " word) acc)))
</lang>
 
Line 1,375 ⟶ 1,372:
 
<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.")
"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)))
</lang>
<pre>
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
Line 1,383 ⟶ 1,384:
Otherwise implement the minimum length greedy algorithm from Wikipedia.
ok
</pre>
<lang lisp>
> (io:format "~s~n" (list (wrap-text test-text 50)))
</lang>
<pre>
Even today, with proportional fonts and complex
layouts, there are still cases where you need to
Line 1,393 ⟶ 1,398:
length greedy algorithm from Wikipedia.
ok
</pre>
>
</lang>
 
=={{header|Lua}}==
Anonymous user