I’m in a class reviewing various languages and we’re building a text parser with Lisp. I can get my Lisp program to do lots of different functions with numbers but I’m struggling with text. I want to just peek at the first character in a line to see if it contains a < then do something, but I can’t seem to figure out how to go about this simple task. Here’s my simple little code so far:
;;;Sets up the y.xml file for use
(setq file (open "c:\\temp\\y.xml"))
;;;Just reads one line at a time, (jkk file)
(defun jkk (x)
(read-line x)
)
;;;Reads the entire file printing each line, (loopfile file)
(defun loopfile (x)
(loop for line = (read-line x nil)
while line do (print line))
)
This next part I tried to combine the loop with an if statement to see if it can find “<” and if so just print that line and skip any others which doesn’t work. Any help with doing this really easy task would be greatly appreciated. Never used Lisp or any other functional language before, I’m used to using functions like crazy in my VB and Java projects but I don’t have any decent reference materials for Lisp.
After this program is done we don’t have to mess with Lisp anymore so I didn’t bother to order anything. Trying Google Books.. starting to figure stuff out but this language is and old and tough one!
;;;Reads the entire file printing the line when < is found
(defun loopfile_xml (x)
(loop for line = (read-line x nil)
while line do
(
if(char= line "<")
(print line)
)
)
)
Thanks guys
First, Lisp is not C or Java – it has different indentation conventions:
and
I would also give the variables meaningful names.
xis not meaningful.The function
char=works on characters. But both arguments in your code are strings. Strings are not characters.#\<is a character. Strings are also arrays, so you can get the first element of a string using the functionaref.If you want to check if a line is just
<, then you can compare the line using the functionstring=with the string"<".The documentation:
char=and related.string=and related.Lisp is old, but still used and it has a lot of interesting concepts.
To learn Lisp is actually not very tough. You can learn the basics of Lisp in a day. If you already know Java, you may need two or even three days.