要实现的while控制结构如下(用C/Java语言描述):
while (test) body;
要实现循环,要么用递归函数,要么用goto。用goto的等效实现如下:
label:
if (test)
{
body;
goto label;
}
LISP的七个基本规则中没有goto,要介绍CLisp的新操作符tagbody
tagbody将多条LISP语句组在一起,按顺序执行,例如
(tagbody
(print 'a)
(print 'b)
(print 'c))
在tagbody里面可以加标签、和go语句
(tagbody
(print 'a)
(go label_x)
(print 'b)
label_x
(print 'c))
上面语句不会打印字母B,注意标签后面不需要冒号
用tagbody实现的while语句
(tagbody
while_label
(cond (test body (go while_label)))
例如打印1~10的循环
x=0
while (x<10) {
++x;
print (x); }
可以实现为
(setq x 0)
(tagbody
while_label
(cond ((< x 10) (setq x (+ 1 x))
(pr