【问题标题】:How do you print two items on the same line in lisp?你如何在 lisp 的同一行上打印两个项目?
【发布时间】:2016-02-03 20:48:57
【问题描述】:

我正在寻找类似的东西:

(printem 1 2)
1 2

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

【问题讨论】:

  • 你说的是哪种 Lisp?
  • 我使用的是 Common Lisp。
  • 为什么要在数字前加上撇号'?非 cons 值(原子)对自己进行评估。

标签: printing format lisp common-lisp


【解决方案1】:

在 Common Lisp 中你可以这样写:

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

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

【讨论】:

    【解决方案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)
    

    【讨论】:

      【解决方案3】:

      你想要的函数可以这样写

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-08-19
        • 1970-01-01
        • 1970-01-01
        • 2017-09-24
        • 2022-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多