1

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?

Thomas Houllier
  • 346
  • 2
  • 10
Samuel Mideksa
  • 423
  • 8
  • 19
  • 1
    If you call (make-cd ...) from the REPL the result is automatically printed, maybe that would be enough for testing. Do you want to print something all the time? – coredump May 04 '19 at 14:45

3 Answers3

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

solves it sorry.

Thomas Houllier
  • 346
  • 2
  • 10
Samuel Mideksa
  • 423
  • 8
  • 19
1

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

Thomas Houllier
  • 346
  • 2
  • 10
  • `format t` is a good way to print list into console. And you are right I got the example form the text book Practical Common Lisp. – Samuel Mideksa May 06 '19 at 07:23
1

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.

Simeon Ikudabo
  • 2,152
  • 1
  • 10
  • 27