<<PART 4 PART 6>>
In the Lisp interpreter, anytime you call a program the return value of the top-level function will be printed on the console. For playing around with testing new functions and stuff, that's all you'll really need, but one of the basic requirements of most programs sooner or later is the ability to read input from a user at the console and to print prompts to the console from a location in the code that is not necessarily the top-level function. For this purpose, there are a couple functions built into Common Lisp that provide you the ability to do simple I/O with the console.
Formatted Output
If you are a user of something like C#, you will probably recognize the following statement:
console.Write("Hello, {0}. \n The Time is {1}", "Stan","1:46");
This is a common way for doing console output, passing a string as a parameter with a couple characters as place holders for arguments to be passed in afterwards (usually these arguments would be derived from another function or calculation, but I hardcoded them for simplicity's sake). Special escape characters like "\n" are used to signify things like new-lines. Lisp output works in much the same way when using the "format" function:
> (format t "Hello, ~A.~% The Time is ~A" "Stan" "1:46")
Hello, Stan.
The Time is 1:46
So first we have the "format" function call, much like any other lisp function. "format" takes a variable number of parameters, but must have a minimum of 2. The first one, "t", indicates where the output will be going to. "t" is used when you want it to go to the default location (in our case, the console). Next is the string literal to be formatted, with any place-holders marked with the "~A" character rather than the more common {0}, {1}, {2} sequence. Also, a newline is indicated with the "~%" character sequence, rather than the more standard "\n". Then you just have to add another parameter for every "~A" you included in the format string, and you're set. As mentioned above, those parameters used to fill in the "~A" place holders are usually a result of a function call or something, not just hardcoded strings.
Reading console input
Lisp makes the collection of input from the console line incredibly intuitive. How intuitive, you ask? Check it out:
> (read)
That's it. The read function will halt the program until it recieves user input, at which point it will return that input as it's return value. This makes interactive functions very easy to write. Here's a simple example:
(defun greet nil
(format t "What is your name, user?~%")
(format t "Hello, ~A" (read)))
This simple function prints a prompt and then waits for input with the "read" function being used to collect input to feed to the "format" function on the third line. Simple and clean.

3 comments:
And you can do a read eval print loop (REPL) with:
(loop do (print (eval (read))))
How common is {n}? I've never seen it before.
In C#, {n} is the place holder in strings to be formatted. I think java uses %s or %d or %x, etc, depending on the datatype.
Post a Comment