In Common Lisp strings are vectors of characters. They are written as characters surrounded by double quotes (e.g "Hello World!").
The stringp
predicate will return true only if the given value is a string.
Strings can be split into sub-strings with the subseq
function which takes the string a start index and optionally an end index.
The indexing is zero-based.
(subseq "Hello World" 1 5) ; => "ello"
(subseq "skip to the end" 5) ; => "to the end"
Strings can be compared for equality with string=
.
If case of the characters should be ignored then string-equal
can be used.
(string= "macaroni" "macaroni") ; => T
(string= "macaroni" "MACARONI") ; => NIL
(string-equal "macaroni" "MACARONI") ; => T
Operations like upper-casing, lower-casing and capitalization of strings can be done with string-upcase
string-downcase
and string-capitalize
.
(string-upcase "not too loud") ; => "NOT TOO LOUD"
(string-downcase "YELL IT FROM THE ROOFTOPS") ; => "yell it from the rooftops"
(string-capitalize "practical COMMON LiSP") ; => "Practical Common Lisp"
In this exercise you will write some code to help Larry the Lisp Alien parse and format log strings. These log strings come in from various systems and need to be processed so that their messages can be display on an informative monitoring display.
Log strings are in the form "[<level>]: <message>"
. Level will be one of the following values: info
, warn
, or ohno
.
Define a function log-message
which will take a log string and evaluate to the message it contains.
(log-message "[ohno]: whoops!") ; => "whoops!"
The level of the log string determines its severity.
info
means a severity of :everything-ok
warn
means a severity of :getting-worried
ohno
means a severity of :run-for-cover
Define a function log-severity
which will take a log string and evaluate to the correct severity.
(log-severity "[warn]: might want to get that checked") ; => :getting-worried
Unfortunately sometimes the log strings are not always formatted correctly. Specifically the log level may be not all lower case as specified above. Modify log-severity
to handle this.
(log-severity "[WaRn]: string case system failing") ; => :getting-worried
Depending upon the severity the message should be modified in different ways:
:everything-ok
should have its message changed to all lower-case.:getting-worried
should have its message changed to have each word capitalized.:run-for-cover
should have its message changed to be all upper-case.Define a function log-format
that takes a log message and evaluates to a properly formatted message.
(log-format "[ohno]: whoops!") ; => WHOOPS!
Sign up to Exercism to learn and master Common Lisp with 27 concepts, 85 exercises, and real human mentoring, all for free.