Printing a list of lists

Discussion of Common Lisp
Post Reply
samohtvii
Posts: 12
Joined: Thu Aug 23, 2012 11:49 pm

Printing a list of lists

Post by samohtvii » Tue Sep 11, 2012 8:40 pm

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

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: Printing a list of lists

Post by nuntius » Tue Sep 11, 2012 9:41 pm

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

pjstirling
Posts: 166
Joined: Sun Nov 28, 2010 4:21 pm

Re: Printing a list of lists

Post by pjstirling » Wed Sep 12, 2012 1:08 pm

Is there a reason that you don't want to use hash-tables for this? (or alists)

samohtvii
Posts: 12
Joined: Thu Aug 23, 2012 11:49 pm

Re: Printing a list of lists

Post by samohtvii » Wed Sep 12, 2012 8:41 pm

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

wvxvw
Posts: 127
Joined: Sat Mar 26, 2011 6:23 am

Re: Printing a list of lists

Post by wvxvw » Sat Sep 15, 2012 11:00 am

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

Konfusius
Posts: 62
Joined: Fri Jun 10, 2011 6:38 am

Re: Printing a list of lists

Post by Konfusius » Fri Sep 21, 2012 12:06 pm

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

samohtvii
Posts: 12
Joined: Thu Aug 23, 2012 11:49 pm

Re: Printing a list of lists

Post by samohtvii » Fri Sep 21, 2012 5:57 pm

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.

Post Reply