Samuel Mideksa
Samuel Mideksa

Reputation: 465

How to call a function that prints a list in lisp?

I am trying to call a function in lisp that assigns it's parameters to a list and prints them to the console but is is not printing anything to the console. The code looks like the following

(defun make-cd (title artist rating ripped)
  (list :title title :artist artist :rating rating :ripped ripped))
(make-cd "Roses" "Kathy Mattea" 7 t)

A call to a make-cd function should return

(:TITLE "Roses" :ARTIST "Kathy Mattea" :RATING 7 :RIPPED T)

How can I fix this problem?

Upvotes: 1

Views: 2752

Answers (3)

Simeon Ikudabo
Simeon Ikudabo

Reputation: 2190

You can simply return the value by pushing it to a list of CD's, which I believe the example in the book your using does initially (and later you'll format each CD in your database when you print them):

(defun make-cd (artist title rating ripped)
   (push (list :artist artist :title title :rating rating :ripped ripped)
     *cds*))

So if I call the function it'll return the contents of the CD to the console:

(make-cd "Cece Winans" "Mercy Said No" 10 t)
((:ARTIST "Cece Winans" :TITLE "Mercy Said No" :RATING 10 :RIPPED T))

The value is returned to the console in the case for the CD you push to the database of CD's.

Upvotes: 1

Samuel Mideksa
Samuel Mideksa

Reputation: 465

(defun make-cd (title artist rating ripped)
  (print (list :title title :artist artist :rating rating :ripped ripped)))

solves it sorry.

Upvotes: 1

Thomas Houllier
Thomas Houllier

Reputation: 346

You can look here: What's the difference between write, print, pprint, princ, and prin1?

format can also be used to print lists, in the REPL or in any output streams (files, pipes etc.).

(format t "~a" (list "Peter" 15 "Steven" 59.4d0))
    => (Peter 15 Steven 59.4d0)

You can go over the material in the CLHS: http://www.lispworks.com/documentation/lw50/CLHS/Body/f_format.htm Or in Practical Common Lisp, from which you got your example I believe: http://www.gigamonkeys.com/book/a-few-format-recipes.html

Upvotes: 1

Related Questions