Page 1 of 1

Printing a list of lists

Posted: Tue Sep 11, 2012 8:40 pm
by samohtvii
I made a database named db. db contains lists.

db = ((a list)(a list)(a list))
a list = (:Question question :Answer answer)

How can I print the question only and not the answer.

(car (car db)) gives me "QUESTION" and not the data in question.

I am pretty new to LISP so, yeah...

Even better then 2 cars would be a way to say (car (db:Question) or something.

What i really want to do is

(dolist (data db:question)
(do something))

Thanks

Re: Printing a list of lists

Posted: Tue Sep 11, 2012 9:41 pm
by nuntius
This might give you some ideas.

Code: Select all

(let ((db '((1 2) (1 2 3 4))))
  (dolist (data db)
    (print (loop for x in data by #'cddr
                 collecting x))))

(let ((db '((1 2) (1 2 3 4))))
  (getf (cadr db) 3))

(let ((db '((1 2) (1 2 3 4))))
  (dolist (data db)
    (print (loop for (key . val) on data by #'cddr
                 collecting key))))

Re: Printing a list of lists

Posted: Wed Sep 12, 2012 1:08 pm
by pjstirling
Is there a reason that you don't want to use hash-tables for this? (or alists)

Re: Printing a list of lists

Posted: Wed Sep 12, 2012 8:41 pm
by samohtvii
nuntius wrote:This might give you some ideas.

Code: Select all

(let ((db '((1 2) (1 2 3 4))))
  (dolist (data db)
    (print (loop for x in data by #'cddr
                 collecting x))))

(let ((db '((1 2) (1 2 3 4))))
  (getf (cadr db) 3))

(let ((db '((1 2) (1 2 3 4))))
  (dolist (data db)
    (print (loop for (key . val) on data by #'cddr
                 collecting key))))
Thanks for the help. The only one I could 'understand' is the getf. The other 2 I had a hard time understanding.
So i am looping in the dolist to get all of the first list. Then i need to print a loop that gets the data from the key question?
I get an error saying "Car: :question is not a list." I thought question would be hidden and the data would 'overlay' that key.

Any functions I should look up? getf was a good start.

best i have so far is:

Code: Select all

(dolist (data (car *db*))
(print (get 'data ':question)))
That prints out NIL and not the question

Re: Printing a list of lists

Posted: Sat Sep 15, 2012 11:00 am
by wvxvw
One more way to do it:

Code: Select all

(defun example ()
  (loop for (a b) in '((:question "A?" :answer "A!")
                      (:question "B?" :answer "B!")
                      (:question "C?" :answer "C!"))
       do (format t "a: ~s, b: ~s~&" a b)))
This is called destructuring bind, here's the reference to it: http://www.cs.cmu.edu/Groups/AI/html/cl ... de252.html

Re: Printing a list of lists

Posted: Fri Sep 21, 2012 12:06 pm
by Konfusius
The easiest way to parse the database entries is to use destructuring-bind:

Code: Select all

(dolist (x '((:question "A?" :answer "A!")
             (:question "B?" :answer "B!")
             (:question "C?" :answer "C!")))
  (destructuring-bind (&key question answer) x
    (format t ":question ~s :answer ~s~%" question answer)))

Re: Printing a list of lists

Posted: Fri Sep 21, 2012 5:57 pm
by samohtvii
Sorry for the confusion but the DB contains an unknown amount of lists so I can't just print them all out one by one, like i think the last 2 suggestions where.