你如何在 lisp 的同一行上打印两个项目?

How do you print two items on the same line in lisp?

我正在寻找类似的东西:

(printem 1 2)
1 2

我假设您是通过格式调用来执行此操作的,但这些示例并不关注此。或者也许你写一个字符串并输出它?但这似乎也不对。

在 Common Lisp 中你可以这样写:

(format t "~d ~d~%" 1 2)

请参阅 Peter Seibel 的 Practical Common Lisp 中的 A Few FORMAT Recipes(您可能也会对其他章节感兴趣)。

你想要的函数可以这样写

(defun printem (&rest args)
    (dolist (el args) 
        (princ el) 
        (princ #\SPACE)))

>> (printem 1 2)
1 2 

您可以简单地构建一个函数,通过 iteration construct 格式打印其所有参数。

(defun printem (&rest args)
  (format t "~{~a~^ ~}" args))

用法:

CL-USER> (printem 1 2 3)
1 2 3
CL-USER> (printem '(1 2 3) '(4 5 6))
(1 2 3) (4 5 6)