【问题标题】:Confused about format function in elisp对elisp中的格式功能感到困惑
【发布时间】:2012-06-18 03:08:41
【问题描述】:

我想得到像 0 1 这样的输出,但下面的代码只是打印 nil。我使用type-of来测试(第一个十六进制),它是整数。 %d 应该可以,对吧?如果我使用消息,它可以在 Emacs 中使用。

(defun draw-board (board)
  (loop for x below 2
        for hex = (aref board x)
        do (format "%d " (first hex))))

(draw-board [(0 2) (1 2) (0 3) (0 2)])

【问题讨论】:

    标签: emacs lisp elisp


    【解决方案1】:

    1- emacs lisp 格式不是 Common Lisp 格式。注意失踪 论据!

    (format "%d" 42) == (cl:format NIL "~D" 42)
    

    2- 因此,您的循环所做的唯一事情是:

     - to check that board is a vector with at least two slots. (aref
       signals an error if the index is out of bound).
    
     - to check that each of those two slots are lists (first signals an
       error when passed a non list).
    
     - to check that the first element of each each of those two slots
       are numbers. (format signals an error when you pass a non number
       for %d).
    

    就是这样。

    你从来没有说过你想打印任何东西。

    要打印一些东西,你必须把它放在一个缓冲区中,然后使用 ps-打印缓冲区:

    (defun draw-board (board)
      (with-temp-buffer
        (loop for x below 2
              for hex = (aref board x)
              do (insert (format "%d " (first hex))))
        (ps-print-buffer)))
    

    【讨论】:

    • 谢谢,我用普通lisp的格式功能误会了。 0_0
    • @wvxvw loop是一个宏,所以只需要使用(eval-when-compile '(require 'cl)),编译不会有运行时惩罚。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 2013-08-21
    • 2013-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多