Printing a list of lists
Printing a list of lists
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
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
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))))
-
- Posts: 166
- Joined: Sun Nov 28, 2010 4:21 pm
Re: Printing a list of lists
Is there a reason that you don't want to use hash-tables for this? (or alists)
Re: Printing a list of lists
Thanks for the help. The only one I could 'understand' is the getf. The other 2 I had a hard time understanding.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))))
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)))
Re: Printing a list of lists
One more way to do it:
This is called destructuring bind, here's the reference to it: http://www.cs.cmu.edu/Groups/AI/html/cl ... de252.html
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)))
Re: Printing a list of lists
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
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.