Open In App

Formatted Output to Character Streams in LISP

Last Updated : 26 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:

 



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads