2012-06-18 44 views
0

我想得到像的輸出,但下面的代碼只是打印。我使用類型來測試(第一個十六進制),它是整數。 %d應該可以工作,對嗎?如果我使用消息,它在Emacs中起作用。關於elisp格式化功能的困惑

(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)]) 

回答

6

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))) 
+0

謝謝,我與Common Lisp中的格式化功能誤會吧。 0_0 – louxiu

+0

@wvxvw'loop'是一個宏,所以你只需要使用'(eval-when-compile'(require'cl))',並且如果你編譯的話沒有運行時間的損失。 –