title: Need advice on how to print a matrix in lisp
body: <p>I have a matrix defined so if I do this</p>
  
  <pre><code>(format t "~a" (get-real-2d 0 0))
  </code></pre>
  
  <p>it prints out the element in the first row first column</p>
  
  <p>and if I do this</p>
  
  <pre><code>(format t "~a" (get-real-2d a 0 1))
  </code></pre>
  
  <p>it prints out the element in first row second column</p>
  
  <p>and if I do this</p>
  
  <pre><code>(format t "~a" (get-real-2d a 1 0))
  </code></pre>
  
  <p>it prints out the element in second row first column.</p>
  
  <p>The matrix <code>a</code> looks like this</p>
  
  <pre><code>a =
  ((0 1 2)
  (3 4 5)
  (6 7 8))
  </code></pre>
  
  <p>and I was hoping you can show me exactly how to write a <code>dotimes</code> loop or other loop 
  that would in as few lines as possible would print out the matrix using the <code>get-real-2d</code> function so the output looks  like this:</p>
  
  <pre><code>0 1 2 
  3 4 5
  6 7 8
  </code></pre>
  
  <p>I'm just hoping you can show me a slick loop that would be real small that I can use to print matrices that I can use in my lisp library something real professional looking, like one that would use only variables. Something like:</p>
  
  <pre><code>(format t "~a" (get-real-2d i j))
  </code></pre>
  
  <p>instead of a bunch of:</p>
  
  <pre><code>(format t "~a" (get-real-2d 0 0))
  (format t "~a" (get-real-2d 0 1))
  (format t "~a" (get-real-2d 0 2))
  </code></pre>
  
  <p>;;;;LATEST EDIT;;;
  to make this simple I call</p>
  
  <pre><code>(defparameter a (create-mat 3 3 +32fc1+))
  </code></pre>
  
  <p>to create a 3x3 matrix - create-mat is a wrapper for opencv's cvCreateMat</p>
  
  <p>the output from that command at repl is</p>
  
  <pre><code>(defparameter a (create-mat 3 3 +32fc1+))
  A
  CL-OPENCV&gt; a
  #.(SB-SYS:INT-SAP #X7FFFD8000E00)
  </code></pre>
  
  <p>i/e the variable a is a pointer to the 3x3 matrix</p>
  
  <p>then I run </p>
  
  <pre><code>(defparameter data (cffi:foreign-alloc :float :initial-contents 
            '(0.0f0 1.0f0 2.0f0 3.0f0 4.0f0 5.0f0 6.0f0 7.0f0 8.0f0)))
  </code></pre>
  
  <p>to create the data for the matrix - which I next will allocate to the matrix</p>
  
  <p>the output from that command at repl is</p>
  
  <pre><code>CL-OPENCV&gt; (defparameter data (cffi:foreign-alloc :float :initial-contents 
            '(0.0f0 1.0f0 2.0f0 3.0f0 4.0f0 5.0f0 6.0f0 7.0f0 8.0f0)))
  DATA
  CL-OPENCV&gt; data
  #.(SB-SYS:INT-SAP #X7FFFD8000E40)
  </code></pre>
  
  <p>i/e the variable a is data pointer to the data ill add to the matrix</p>
  
  <p>then I call..</p>
  
  <pre><code>(set-data a data 12) to add the data to the matrix - set-data is a wrapper for opencv's cvSetData
  </code></pre>
  
  <p>so now when I run - (get-real-2d is a wrapper for opencv's cvGetReal2d)</p>
  
  <pre><code>(get-real-2d a 0 0)  it gets the element of matrix a at row 0 col 0 which is 0.0d0
  </code></pre>
  
  <p>the output from that command at repl is</p>
  
  <pre><code>CL-OPENCV&gt; (get-real-2d a 0 0)
  0.0d0
  </code></pre>
  
  <p>and now when I run</p>
  
  <pre><code>(get-real-2d a 0 1)  it gets the element of matrix a at row 0 col 1 which is is 0.0d0
  </code></pre>
  
  <p>the output from that command at repl is</p>
  
  <pre><code>CL-OPENCV&gt; (get-real-2d a 0 1)
  1.0d0
  </code></pre>
  
  <p>and when I run this loop</p>
  
  <pre><code> (dotimes (i 3)
    (dotimes (j 3)
  (format t "~a~%" (get-real-2d a i j))))
  </code></pre>
  
  <p>the output from that command at repl is</p>
  
  <pre><code>CL-OPENCV&gt; (dotimes (i 3)
    (dotimes (j 3)
  (format t "~a~%" (get-real-2d a i j))))
  0.0d0
  1.0d0
  2.0d0
  3.0d0
  4.0d0
  5.0d0
  6.0d0
  7.0d0
  8.0d0
  NIL
  </code></pre>
  
  <p>but when I try your method @Svante</p>
  
  <pre><code>(dotimes (i 3)
    (dotimes (j 3)
  (format t "~{~{~a~^ ~}~%~}" (get-real-2d a i j))))
  </code></pre>
  
  <p>I get error:</p>
  
  <pre><code>The value 0.0d0 is not of type LIST.
      [Condition of type TYPE-ERROR]
  </code></pre>
  
  <p>because the output  of 1 run of get-real-2d is just a 1 number float i/e</p>
  
  <pre><code>CL-OPENCV&gt; (get-real-2d a 0 0)
  0.0d0
  </code></pre>
  
  <p>with that info can you help me print the matrix so it looks like this</p>
  
  <pre><code>0.0d0 1.0d0 2.0d0 
  3.0d0 4.0d0 5.0d0
  6.0d0 7.0d0 8.0d0
  </code></pre>