LISP程序输出

时间:2015-10-14 01:53:46

标签: lisp output

我是LISP的新手,无法弄清楚以下LISP的作用是什么?

(setq A '(RIGHT ARE YOU))
(print (reverse (list (first (rest A))(first (rest (rest A))) (first A) 'HOW)))

setq分配词汇变量

2 个答案:

答案 0 :(得分:2)

打印:

(HOW RIGHT YOU ARE)

第一行将3个元素的列表分配给符号A。引用此方法是为了防止将(RIGHT ARE YOU)评估为名为RIGHT的函数。第二行做了一些不必要的冗长和复杂的逻辑,基本上创建了一个由四个元素组成的列表:字符串HOWA中的三个元素。

分解第二行:

  • (first (rest A)) - 这会从YOU
  • 返回元素A
  • (first (rest (rest A))) - 这会从ARE
  • 返回元素A
  • (first A) - 这会从A
  • 返回元素'RIGHT'

这现在让你:

(print (reverse (list ARE YOU RIGHT 'HOW)))

你在学习哪个LISP?许多LISP都有一个REPL(读取 - 评估 - 打印循环)的概念,它允许您试验复杂的表达式并将它们分解成更小的块以理解中间步骤的结果。

答案 1 :(得分:1)

也许Common Lisp中REPL会话的这个记录会启发:

CL-USER> (setq a '(right are you))
(RIGHT ARE YOU)
CL-USER> (print (reverse (list (first (rest a)) (first (rest (rest a))) (first a) 'how)))

(HOW RIGHT YOU ARE) 
(HOW RIGHT YOU ARE)
CL-USER> a
(RIGHT ARE YOU)
CL-USER> (rest a)
(ARE YOU)
CL-USER> (cdr a)
(ARE YOU)
CL-USER> (first (rest a))
ARE
CL-USER> (cadr a)
ARE
CL-USER> (rest (rest a))
(YOU)
CL-USER> (cddr a)
(YOU)
CL-USER> (first (rest (rest a)))
YOU
CL-USER> (caddr a)
YOU
CL-USER> (first a)
RIGHT
CL-USER> (car a)
RIGHT
CL-USER> (values (first (rest a)) (first (rest (rest a))) (first a) 'how)
ARE
YOU
RIGHT
HOW
CL-USER> (list (first (rest a)) (first (rest (rest a))) (first a) 'how)
(ARE YOU RIGHT HOW)
CL-USER> (list 'are 'you 'right 'how)
(ARE YOU RIGHT HOW)
CL-USER> (reverse '(are you right how))
(HOW RIGHT YOU ARE)
CL-USER> '(how right you are)
(HOW RIGHT YOU ARE)
CL-USER> (print '(how right you are))

(HOW RIGHT YOU ARE) 
(HOW RIGHT YOU ARE)
CL-USER>