2

quick question.

I'm trying to add something to my function where it prints back whatever args I give it as part of a string to the console.

    (defun test (testvar)
(format Your number is *testvar*))

After looking around i think that I am supposed to use format but that's as far as I found.

1
  • Did you read the documentation for FORMAT? Commented Apr 10, 2016 at 15:37

1 Answer 1

3

You can interact in Common Lisp through a console within a REPL loop (READ-EVAL-PRINT Loop). So every expression is read, evaluated, and the result of the evaluation is printed;

CL-USER> (defun test (testvar)
           (format nil "Your input is ~A" testvar))
TEST
CL-USER> (test 3)
"Your input is 3"
CL-USER> (test 'symbol)
"Your input is SYMBOL"
CL-USER> (test "string")
"Your input is string"
CL-USER> 

The format function (reference) when its second argument is nil, returns as string the result of substituting in its second argument (a format string), special marks, like “~a” “~s”, etc., with the remaining parameters.

If the second parameter of format is instead t or a stream, then the formatted string is output to the stream specified (or, in case of t, to the special *standard-output* stream, that initially is the same as the console), and the result of format is returned (and then printed by the REPL). For instance:

CL-USER> (defun test (testvar)
           (format t "Your input is ~A" testvar))
TEST
CL-USER> (test 3)
Your input is 3
NIL
CL-USER> 

In this case NIL is the result of the format function. Note also that, differently from the first case, in which Your input is 3 is printed surrounded by double quote (since the result of (format nil ...) is a string, and is printed by the REPL as such), the output is left intact from the printing operation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.