Abbreviations, automatic: Difference between revisions

Line 436:
 
=={{header|Common Lisp}}==
It uses the standard library split-sequence to split the string into words.
<lang Common Lisp>
<lang lisp>
(defun split (str &key (delims " ")
(reverse nil)
(missing-value-marker nil))
"Returns a list of strings formed by splitting <str> at any character
contained in <delims>. If <missing-value-marker> is non-nil, then
<missing-value-marker> is used when no text is found between delimiters.
Example: (rsm.string:split \"abc def ghi\" :delims \" \")
(\"abc\" \"def\" \"ghi\")"
(labels
((rec (str acc)
(let ((pos (position-if #'(lambda (ch)
(position ch delims)) str)))
(if pos
(rec (subseq str (1+ pos))
(cond ((> pos 0)
(cons (subseq str 0 pos) acc))
(missing-value-marker
(cons missing-value-marker acc))
(t
acc)))
(if reverse
(cons str acc)
(nreverse (cons str acc)))))))
(delete "" (rec str nil) :test #'string-equal)))
 
 
(defun max-mismatch (list)
(if (cdr list)
(max (apply #'max (mapcar #'(lambda (w2) (mismatch (car list) w2)) (cdr list))) (max-mismatch (cdr list)))
0 ))
 
 
(with-open-file (f "days-of-the-week.txt" :direction :input)
(do* ((row (read-line f nil nil) (read-line f nil nil)))
((null row) t)
(format t "~d ~a~%" (1+ (max-mismatch (SPLIT-SEQUENCE:split-sequence #\Space row))) row) ))
</lang>
 
{{out}}
<pre>2 Sunday Monday Tuesday Wednesday Thursday Friday Saturday
47

edits