LISP If Statement - 解析文本文件

JKK*_*JKK 1 lisp common-lisp

我正在上课,回顾各种语言,我们正在用Lisp构建一个文本解析器.我可以让我的Lisp程序用数字做很多不同的函数,但我正在努力处理文本.我想偷看一行中的第一个字符,看它是否包含<然后做某事,但我似乎无法弄清楚如何去完成这个简单的任务.到目前为止,这是我简单的小代码:

;;;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))
)
Run Code Online (Sandbox Code Playgroud)

下一部分我试图将循环与if语句结合起来,看看它是否可以找到"<",如果是这样,只需打印该行并跳过任何其他不起作用的行.任何帮助做这个非常简单的任务将不胜感激.之前从未使用过Lisp或任何其他函数式语言,我习惯在VB和Java项目中使用疯狂的函数,但我没有任何体面的Lisp参考资料.

完成此程序后,我们不再需要使用Lisp了,所以我没有费心去订购任何东西.尝试使用Google Books ..开始计算出来的东西,但这种语言又古老而又艰难!

;;;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)
    )
)
)
Run Code Online (Sandbox Code Playgroud)

多谢你们

Rai*_*wig 16

首先,Lisp不是C或Java - 它有不同的缩进约定:

;;;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)))
Run Code Online (Sandbox Code Playgroud)

;;;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))))
Run Code Online (Sandbox Code Playgroud)

我也会给变量有意义的名字.x没有意义.

该功能char=适用于角色.但是代码中的两个参数都是字符串.字符串不是字符.#\<是一个角色.字符串也是数组,因此您可以使用该函数获取字符串的第一个元素aref.

如果要检查线是否正好<,则可以将使用该函数的线string=与字符串进行比较"<".

文件:

Lisp很老,但仍然使用,它有很多有趣的概念.

学习Lisp实际上并不是很难.您可以在一天内学习Lisp的基础知识.如果您已经了解Java,则可能需要两天甚至三天.

  • JKK_1979,Common Lisp是一种多范式语言,也可以用于OO或程序风格.以下是在lisps中格式化代码的一个很好的指南:http://mumble.net/~campbell/scheme/style.txt (2认同)