The following function is designed to return the first three paragraphs of a string. (def shortify (i str)
"Shortens a given chunk of text to i paragraphs."
(unless (> i 3)
(iflet var (posmatch "\n\n" str)
(unless (is var "")
(string (cut str 0 (- (+ var 2) (len str)))
(shortify (+ i 1) (cut str (+ var 2))))))))
The following: (shortify 1 "foo\n\nbar\n\nbaz\n\nbing!")
Would return: "foo\n\nbar\n\nbaz\n\n"
The only problem is, it falls on its face with an index out of range error for the string if there are less than three instances of "\n\n" in a string.Any ideas? Also, general cleanup and improvement of the function itself is welcome! The function is super-ugly right now, since I'm focusing on well, getting it to work before I clean up the semantics of it. |