i’ve been reading some lisp code and came across this section, didn’t quite understand what it specifically does, though the whole function is supposed to count how many times the letters from a -z appear in an entered text.
(do ((i #.(char-code #\a) (1+ i)))
((> i #.(char-code #\z)))
can anyone explain step by step what is happening? I know that it’s somehow counting the letters but not quite sure how.
This Lisp code is slightly unusual, since it uses read-time evaluation.
#.exprmeans that the expression will be evaluated only once, during read-time.In this case a clever compiler might have guessed that the character code of a given character is known and could have removed the computation of character codes from the
DOloop. The author of that code chose to do that by evaluating the expressions before the compiler sees it.The source looks like this:
When Lisp reads in the s-expression, we get this new code as the result (assuming a usual encoding of characters):
So that’s a loop which counts the variable
iup from 97 to 122.