Renata P Souza
Renata P Souza

Reputation: 255

print nested lists in columns in Lisp

I have a doubt on how to print several matrix-like nested lists in a row, row after row, vertically aligned so they appear in columns one under the other.

I tried several ways but none of it works and I've used several text formats but it's not coming out as expected. any suggestions?

I updated the doubt and I hope that now you can understand.

(defun tabA()
  '((X 0 0 0 0 0 0) 
    (0 0 0 X 0 0 0) 
    (0 0 0 0 0 0 0) 
    (0 0 0 T 0 0 0)  
    (0 0 0 0 0 0 0)))

(defun tabB()
  '((0 0 0 0 X 0 0) 
    (0 0 0 0 0 0 0) 
    (0 T 0 0 0 0 0) 
    (0 0 0 X 0 0 0)  
    (0 0 0 0 0 0 0)))

(defun tabC()
  '((0 0 0 T 0 0 0) 
    (0 0 0 0 0 0 0) 
    (0 0 X 0 0 X 0) 
    (0 0 0 0 0 0 0)  
    (0 0 0 0 0 0 0)))

(defun states ()
  (list (tabA) (tabB) (tabC)))

(defun test-print ()
  (format T "~%--- Result ---")
  (mapcar #'(lambda(x) (print-state x)) (states)))

(defun print-state (x)
  (format T "~%")
  (mapcar (lambda (x) (format T "~@T~@T~@T~@T~@T ~A ~%" x)) x)
  (format NIL ""))

  ;;test print
  (test-print)

you can test and see the result in ideone.com like this example

--- Result ---
  (X 0 0 0 0 0 0) 
  (0 0 0 X 0 0 0) 
  (0 0 0 0 0 0 0) 
  (0 0 0 T 0 0 0) 
  (0 0 0 0 0 0 0) 

  (0 0 0 0 X 0 0) 
  (0 0 0 0 0 0 0) 
  (0 T 0 0 0 0 0) 
  (0 0 0 X 0 0 0) 
  (0 0 0 0 0 0 0) 

  (0 0 0 T 0 0 0) 
  (0 0 0 0 0 0 0) 
  (0 0 X 0 0 X 0) 
  (0 0 0 0 0 0 0) 
  (0 0 0 0 0 0 0) 

and I want to print these lists in two or three columns, like this example

--- Result ---
(X 0 0 0 0 0 0)  (0 0 0 0 X 0 0) 
(0 0 0 X 0 0 0)  (0 0 0 0 0 0 0) 
(0 0 0 0 0 0 0)  (0 T 0 0 0 0 0) 
(0 0 0 T 0 0 0)  (0 0 0 X 0 0 0) 
(0 0 0 0 0 0 0)  (0 0 0 0 0 0 0)

(0 0 0 T 0 0 0) 
(0 0 0 0 0 0 0) 
(0 0 X 0 0 X 0) 
(0 0 0 0 0 0 0) 
(0 0 0 0 0 0 0)

Upvotes: 2

Views: 502

Answers (1)

ad absurdum
ad absurdum

Reputation: 21321

If input is regular as in the example data, this can be done simply enough by looping over the row indices of the tables and looping over the tables themselves to print the rows in sequence:

(defun print-tables (tables)
  (let ((rows (length (first tables))))
    (loop for n below rows do
         (terpri)
         (dolist (table tables)
           (format t "~A  " (elt table n))))))

The print-tables function takes a list of tables as its argument. Here it is assumed that the tables are all the same shape with regular column widths. The number of rows is calculated for the first table, and the loop counts row indices starting with 0, printing a newline with (terpri) before each sequence of table rows is printed. The dolist iterates over the tables and for each table the nth row is printed. The same approach could be used for more irregular data, with some additional formatting code.

SCRATCH> (print-tables (list (tabA) (tabB) (tabC)))

(X 0 0 0 0 0 0)  (0 0 0 0 X 0 0)  (0 0 0 T 0 0 0)  
(0 0 0 X 0 0 0)  (0 0 0 0 0 0 0)  (0 0 0 0 0 0 0)  
(0 0 0 0 0 0 0)  (0 T 0 0 0 0 0)  (0 0 X 0 0 X 0)  
(0 0 0 T 0 0 0)  (0 0 0 X 0 0 0)  (0 0 0 0 0 0 0)  
(0 0 0 0 0 0 0)  (0 0 0 0 0 0 0)  (0 0 0 0 0 0 0)  
NIL

The above code just prints the tables in sequence in a single row of tables, but for larger numbers of tables it would be nice to group the tables sensibly. A group-tables function can be written and used in a print-groups function making use of the above print-tables function.

(defun group-tables (grouping tables)
  (labels ((iter (tables group)
             (cond ((null tables)
                    (list group))
                   ((= (length group) grouping)
                    (cons group
                          (iter tables '())))
                   (t
                    (iter (rest tables)
                          (append group (list (first tables))))))))
    (iter tables '())))

Here group-tables takes a list of tables and an integer argument specifying the maximum grouping desired per output line. The local iter function builds up a list of groups, but each group is constructed in an accumulator as progress is made; note that append is used here to keep the tables in the same order as input.

(defun print-groups (grouping tables)
  (dolist (group (group-tables grouping tables))
    (print-tables group)
    (terpri)))

Here print-groups just uses group-tables to collect the tables into groups, then uses dolist to pick out each individual group for processing by the print-tables function.

Sample REPL interactions:

SCRATCH> (print-groups 3 (list (tabA) (tabB) (tabC)))

(X 0 0 0 0 0 0)  (0 0 0 0 X 0 0)  (0 0 0 T 0 0 0)  
(0 0 0 X 0 0 0)  (0 0 0 0 0 0 0)  (0 0 0 0 0 0 0)  
(0 0 0 0 0 0 0)  (0 T 0 0 0 0 0)  (0 0 X 0 0 X 0)  
(0 0 0 T 0 0 0)  (0 0 0 X 0 0 0)  (0 0 0 0 0 0 0)  
(0 0 0 0 0 0 0)  (0 0 0 0 0 0 0)  (0 0 0 0 0 0 0)  
NIL
SCRATCH> (print-groups 2 (list (tabA) (tabB) (tabC)))

(X 0 0 0 0 0 0)  (0 0 0 0 X 0 0)  
(0 0 0 X 0 0 0)  (0 0 0 0 0 0 0)  
(0 0 0 0 0 0 0)  (0 T 0 0 0 0 0)  
(0 0 0 T 0 0 0)  (0 0 0 X 0 0 0)  
(0 0 0 0 0 0 0)  (0 0 0 0 0 0 0)  

(0 0 0 T 0 0 0)  
(0 0 0 0 0 0 0)  
(0 0 X 0 0 X 0)  
(0 0 0 0 0 0 0)  
(0 0 0 0 0 0 0)  
NIL
SCRATCH> (print-groups 3 (list (tabA) (tabB) (tabC) (tabA) (tabB) (tabC)))

(X 0 0 0 0 0 0)  (0 0 0 0 X 0 0)  (0 0 0 T 0 0 0)  
(0 0 0 X 0 0 0)  (0 0 0 0 0 0 0)  (0 0 0 0 0 0 0)  
(0 0 0 0 0 0 0)  (0 T 0 0 0 0 0)  (0 0 X 0 0 X 0)  
(0 0 0 T 0 0 0)  (0 0 0 X 0 0 0)  (0 0 0 0 0 0 0)  
(0 0 0 0 0 0 0)  (0 0 0 0 0 0 0)  (0 0 0 0 0 0 0)  

(X 0 0 0 0 0 0)  (0 0 0 0 X 0 0)  (0 0 0 T 0 0 0)  
(0 0 0 X 0 0 0)  (0 0 0 0 0 0 0)  (0 0 0 0 0 0 0)  
(0 0 0 0 0 0 0)  (0 T 0 0 0 0 0)  (0 0 X 0 0 X 0)  
(0 0 0 T 0 0 0)  (0 0 0 X 0 0 0)  (0 0 0 0 0 0 0)  
(0 0 0 0 0 0 0)  (0 0 0 0 0 0 0)  (0 0 0 0 0 0 0)  
NIL
SCRATCH> (print-groups 4 (list (tabA) (tabB) (tabC) (tabA) (tabB) (tabC)))

(X 0 0 0 0 0 0)  (0 0 0 0 X 0 0)  (0 0 0 T 0 0 0)  (X 0 0 0 0 0 0)  
(0 0 0 X 0 0 0)  (0 0 0 0 0 0 0)  (0 0 0 0 0 0 0)  (0 0 0 X 0 0 0)  
(0 0 0 0 0 0 0)  (0 T 0 0 0 0 0)  (0 0 X 0 0 X 0)  (0 0 0 0 0 0 0)  
(0 0 0 T 0 0 0)  (0 0 0 X 0 0 0)  (0 0 0 0 0 0 0)  (0 0 0 T 0 0 0)  
(0 0 0 0 0 0 0)  (0 0 0 0 0 0 0)  (0 0 0 0 0 0 0)  (0 0 0 0 0 0 0)  

(0 0 0 0 X 0 0)  (0 0 0 T 0 0 0)  
(0 0 0 0 0 0 0)  (0 0 0 0 0 0 0)  
(0 T 0 0 0 0 0)  (0 0 X 0 0 X 0)  
(0 0 0 X 0 0 0)  (0 0 0 0 0 0 0)  
(0 0 0 0 0 0 0)  (0 0 0 0 0 0 0)  
NIL

Upvotes: 2

Related Questions