0

So I'm trying to figure out why the following produces unexpected output:

(setq var 1)
(pp var)
(princ var)

Put the cursor just after the closing ) in the expression (setq var 1) and then press C-xC-e to evaluate it. I see the following in the echo area: 1 (#o1 #x1 ?\C-a). I think this makes sense - it's just listing the number 1 several different ways.

I go on to evaluate (C-xC-e again) (pp var) and then see the following: 1"1"

Why do I see the 1 twice?

Why is it listed inside the double-quotes the second time

Likewise, when I go on to evaluate (C-xC-e) (princ var) and then see the following: 11 (#o1 #x1 ?\C-a)

Why is there an 11 at the start? I'm guessing that it's printing the value 1 twice, right next to each other (instead of printing the number 11), but why is it printing 1 twice?

3
  • 1
    Please always state clearly how you are "evaluating" something. As @xuchunyang's answer says, you are no doubt not just evaluating the sexp. You are also printing its value, and the result of your command is being echoed.
    – Drew
    Commented Jun 28, 2018 at 16:01
  • Whoops - sorry, I didn't know that would be an important detail. Is specifying C-xC-e (like I did above) clear? Commented Jun 29, 2018 at 0:40
  • Yes, as the accepted answer indicates. What you see is not due to just evaluation.
    – Drew
    Commented Jun 29, 2018 at 3:19

1 Answer 1

2

Evaluating(princ 1) with C-x C-e (eval-last-sexp) displays the following in the echo area

11 (#o1 #x1 ?\C-a)

because (princ 1) prints 1 and returns the integer 1. And (pp 1) is the same as (princ "1"), it prints 1 and returns the string "1".


Try executing them in M-x ielm, it produces cleaner output.

*** Welcome to IELM ***  Type (describe-mode) for help.
ELISP> (setq var 1)
1 (#b1, #o1, #x1, ?\C-a)
ELISP> (pp var)
1
"1"
ELISP> (princ var)
1
1 (#b1, #o1, #x1, ?\C-a)
ELISP> 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.