Formatted Output to Character Streams in LISP
Last Updated :
26 Jul, 2022
Lisp is a programming language that has an overall style that is organized around expressions and functions. Every Lisp procedure is a function, and when called, it returns a data object as its value. It is also commonly referred to as “functions” even though they may have side effects.
Formatted Output to Character Streams:
The function format is used to produce nice formatted text, good-looking messages, and so on. the format can generate a string or output to a stream.
Syntax:
format destination control-string &rest arguments
where,
destination -> standard output
control-string -> hold the characters that to output and printing directive.
A format directive consists of a tilde (~), optional prefix parameters separated by commas, an optional colon (:) and at-sign (@) modifiers, and a single character indicating the type of directive.
The prefix parameters are generally integers, notated as optionally signed decimal numbers. The following commonly used directives used for output streams are:
S.No. | Directives | Description |
---|
1 | ~B | For binary arguments. |
---|
2 | ~D | For decimal arguments. |
---|
3 | ~S | Is followed by S-expressions. |
---|
4 | ~A | Is followed by ASCII arguments. |
---|
5 | ~O | For octal arguments. |
---|
6 | ~$ | Dollar and floating point arguments. |
---|
7 | ~E | Exponential floating-point arguments. |
---|
8 | ~F | For Fixed-format floating-point arguments. |
---|
9 | ~C | For character arguments. |
---|
10 | ~X | For hexadecimal arguments. |
---|
11 | ~* | The next argument is ignored. |
---|
12 | ~? | Indirection. The next argument must be a string, and the one after it a list. |
---|
13 | ~% | A new line is printed. |
---|
14 | ~^ | Up and out. This is an escape construct. |
---|
15 | ~> | Terminates a ~<. It is an error elsewhere. |
---|
16 | ~} | This terminates a ~{. It is an error elsewhere. |
---|
17 | ~; | This separates clauses in ~[ and ~< constructions. It is an error elsewhere. |
---|
18 | ~(str~) | Case conversion. |
---|
19 | ~[str0~;str1~;...~;strn~] | Conditional expression. This is a set of control strings, called clauses. |
---|
20 | ~W | Write. In an argument, a Lisp object is printed obeying every printer control variable. |
---|
Example 1:
Lisp
; Formatted Output to Character Streams in LISP
(format t "Hello, World!")
(format nil "foo")
(setq x 10)
(format nil "The answer is ~D." x)
(format nil "The answer is ~3D." x)
(format nil "The answer is ~3,'0D." x)
(format nil "The answer is ~:D." (expt 47 x))
(setq y "cat")
(format nil "Look at the ~A!" y)
(setq n 3)
(format nil "~D item~:P found." n)
(format nil "~R dog~:[s are~; is~] here." n (= n 1))
(format nil "~R dog~:*~[s are~; is~:;s are~] here." n)
(format nil "Here ~[are~;is~:;are~] ~:*~R pupp~:@P." n)
Output:
Example 2:
Lisp
;Formatted Output to Character Streams in LISP
(defun perimeterofsquare()
(terpri)
(princ "Enter side length: ")
(setq length (read))
(setq peri (* 4 length))
(format t "Length: = ~F~% Perimeter = ~F" length peri)
)
(perimeterofsquare)
Output:
Similar Reads
Input From Character Streams in LISP This article is about input from character streams in LISP. Input in LISP can be taken in different ways like input from the user, input from character streams, etc. In LISP input from character streams can be taken from different functions accordingly. Input from character streams can be a string,
3 min read
Convert a String to a List of Characters in Java In Java, to convert a string into a list of characters, we can use several methods depending on the requirements. In this article, we will learn how to convert a string to a list of characters in Java.Example:In this example, we will use the toCharArray() method to convert a String into a character
3 min read
Characters in LISP In Lisp data objects of type 'character' are referred to as characters. For representation purposes, we usually denote character objects by preceding a #\ symbol before the character. Any character can be represented by using the #\ symbol before the name of the character. For Example #\a represents
2 min read
Output Functions in LISP Output functions need an optional argument known as output stream which by default is "output-stream" and can be assigned to another stream where the output has to be sent. Write:write object  key : stream Example 1: Lisp ; LISP program for Write (write "Hello World") (write "To") (write "The") (wr
3 min read
Insert a character in String at a Given Position Given a string s, a character c and an integer position pos, the task is to insert the character c into the string s at the specified position pos.Examples: Input: s = "Geeks", c = 'A', pos = 3Output: GeeAksInput: s = "HelloWorld", c = '!', pos = 5Output: Hello!WorldTable of Content[Approach-1] Usin
5 min read