2

When I have a function definition make-cd and execute the function to get the wrong answer.

(defun make-cd (title artist rating ripped)
  '(:title title :artist artist :rating rating :ripped ripped))

(add-record (make-cd "Roses" "Kathy Mattea" 7 t))

(:TITLE TITLE :ARTIST ARTIST :RATING RATING :RIPPED RIPPED)

I should have used (list ...) to get a correct answer.

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

(add-record (make-cd "Roses" "Kathy Mattea" 7 t))

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

Why is this?

prosseek
  • 182,215
  • 215
  • 566
  • 871
  • `'(X Y Z)` is shorthand for `(QUOTE (X Y Z))`. [`QUOTE`](http://www.lispworks.com/documentation/HyperSpec/Body/s_quote.htm) simply returns the given object as literal data instead of evaluating it. That is, in your example `TITLE`, `ARTIST`, `RATING` and `RIPPED` aren't references to the variables with those names; they are just literal symbols. – jkiiski Jun 19 '16 at 18:09
  • It's like the difference between constant strings that don't interpolate `'this is $WYSIWYG' // => 'this is $WYSIWYG'` and strings that do `"This is not $WYSIWYG" // => "This is not replaced"`. – Sylwester Jun 19 '16 at 21:12

1 Answers1

4

Lisp has symbols as a data structure. Symbols can be used as themselves - as symbols - or as variables in code.

You need to remember the evaluation rules for quoted expressions and function calls:

Evaluation rule for quoted expression: nothing inside a quoted expression is evaluated. The value is returned as is.

Evaluation rule for function calls: with a function call, all arguments are evaluated left-to-right and these results passed to the function. The computation results of the function are being returned.

Creating data:

A quoted symbol:

CL-USER 13 > 'foo
FOO

A quoted list. Nothing inside the quote is evaluated.

CL-USER 14 > '(foo bar)
(FOO BAR)

A nested list, quoted.

CL-USER 15 > '((foo bar) (foo baz))
((FOO BAR) (FOO BAZ))

A freshly created list using the function list. The contents are symbols.

CL-USER 16 > (list 'foo 'bar)
(FOO BAR)

A freshly created nested list:

CL-USER 17 > (list (list 'foo 'bar) (list 'foo 'bar))
((FOO BAR) (FOO BAR))

A freshly created list, using quoted lists as contents:

CL-USER 18 > (list '(foo bar) '(foo bar))
((FOO BAR) (FOO BAR))

Creating data, using variables:

Using the function list with two variables:

CL-USER 19 > (let ((foo 1)
                   (bar 2))
               (list foo bar))
(1 2)

Using a backquoted list. The elements after the comma are evaluated.

CL-USER 20 > (let ((foo 1)
                   (bar 2))
               `(,foo ,bar))
(1 2)

Using a nested backquoted list. The elements after the comma are evaluated.

CL-USER 21 > (let ((foo 1)
                   (bar 2))
               `((,foo ,bar) (,foo ,bar)))
((1 2) (1 2))
Rainer Joswig
  • 136,269
  • 10
  • 221
  • 346