📅  最后修改于: 2020-11-03 07:19:16             🧑  作者: Mango
常见的LISP提供许多输入输出功能。我们已经使用了format函数和print函数进行输出。在本节中,我们将研究LISP中提供的一些最常用的输入输出功能。
下表提供了LISP最常用的输入功能-
Sr.No. | Function & Description |
---|---|
1 |
read & optional input-stream eof-error-p eof-value recursive-p It reads in the printed representation of a Lisp object from input-stream, builds a corresponding Lisp object, and returns the object. |
2 |
read-preserving-whitespace & optional in-stream eof-error-p eof-value recursive-p It is used in some specialized situations where it is desirable to determine precisely what character terminated the extended token. |
3 |
read-line & optional input-stream eof-error-p eof-value recursive-p It reads in a line of text terminated by a newline. |
4 |
read-char & optional input-stream eof-error-p eof-value recursive-p It takes one character from input-stream and returns it as a character object. |
5 |
unread-char character & optional input-stream It puts the character most recently read from the input-stream, onto the front of input-stream. |
6 |
peek-char & optional peek-type input-stream eof-error-p eof-value recursive-p It returns the next character to be read from input-stream, without actually removing it from the input stream. |
7 |
listen & optional input-stream The predicate listen is true if there is a character immediately available from input-stream, and is false if not. |
8 |
read-char-no-hang & optional input-stream eof-error-p eof-value recursive-p It is similar to read-char, but if it does not get a character, it does not wait for a character, but returns nil immediately. |
9 |
clear-input & optional input-stream It clears any buffered input associated with input-stream. |
10 |
read-from-string string & optional eof-error-p eof-value & key :start :end :preserve-whitespace It takes the characters of the string successively and builds a LISP object and returns the object. It also returns the index of the first character in the string not read, or the length of the string (or, length +1), as the case may be. |
11 |
parse-integer string & key :start :end :radix :junk-allowed It examines the substring of string delimited by :start and :end (default to the beginning and end of the string). It skips over whitespace characters and then attempts to parse an integer. |
12 |
read-byte binary-input-stream & optional eof-error-p eof-value It reads one byte from the binary-input-stream and returns it in the form of an integer. |
读取函数用于从键盘获取输入。可能没有任何论据。
例如,考虑代码片段-
(write ( + 15.0 (read)))
假设用户从STDIN输入中输入10.2,它将返回,
25.2
read函数从输入流中读取字符,并通过解析为Lisp对象的表示来解释它们。
创建一个名为main.lisp的新源代码文件,并在其中键入以下代码-
; the function AreaOfCircle
; calculates area of a circle
; when the radius is input from keyboard
(defun AreaOfCircle()
(terpri)
(princ "Enter Radius: ")
(setq radius (read))
(setq area (* 3.1416 radius radius))
(princ "Area: ")
(write area))
(AreaOfCircle)
当您执行代码时,它返回以下结果-
Enter Radius: 5 (STDIN Input)
Area: 78.53999
创建一个名为main.lisp的新源代码文件,然后在其中键入以下代码。
(with-input-from-string (stream "Welcome to Tutorials Point!")
(print (read-char stream))
(print (read-char stream))
(print (read-char stream))
(print (read-char stream))
(print (read-char stream))
(print (read-char stream))
(print (read-char stream))
(print (read-char stream))
(print (read-char stream))
(print (read-char stream))
(print (peek-char nil stream nil 'the-end))
(values)
)
当您执行代码时,它返回以下结果-
#\W
#\e
#\l
#\c
#\o
#\m
#\e
#\Space
#\t
#\o
#\Space
LISP中的所有输出函数都使用一个称为output-stream的可选参数,将输出发送到该参数。如果未提及或为nil,则output-stream默认为变量* standard-output *的值。
下表提供了LISP最常用的输出功能-
Sr.No. | Function and Description |
---|---|
1 |
write object & key :stream :escape :radix :base :circle :pretty :level :length :case :gensym :array write object & key :stream :escape :radix :base :circle :pretty :level :length :case :gensym :array :readably :right-margin :miser-width :lines :pprint-dispatch Both write the object to the output stream specified by :stream, which defaults to the value of *standard-output*. Other values default to the corresponding global variables set for printing. |
2 |
prin1 object & optional output-stream print object & optional output-stream pprint object & optional output-stream princ object & optional output-stream All these functions outputs the printed representation of object to output-stream. However, the following differences are there −
|
3 |
write-to-string object & key :escape :radix :base :circle :pretty :level :length :case :gensym :array write-to-string object & key :escape :radix :base :circle :pretty :level :length :case :gensym :array :readably :right-margin :miser-width :lines :pprint-dispatch prin1-to-string object princ-to-string object The object is effectively printed and the output characters are made into a string, which is returned. |
4 |
write-char character & optional output-stream It outputs the character to output-stream, and returns character. |
5 |
write-string string & optional output-stream & key :start :end It writes the characters of the specified substring of string to the output-stream. |
6 |
write-line string & optional output-stream & key :start :end It works the same way as write-string, but outputs a newline afterwards. |
7 |
terpri & optional output-stream It outputs a newline to output-stream. |
8 |
fresh-line & optional output-stream it outputs a newline only if the stream is not already at the start of a line. |
9 |
finish-output & optional output-stream force-output & optional output-stream clear-output & optional output-stream
|
10 |
write-byte integer binary-output-stream It writes one byte, the value of the integer. |
创建一个名为main.lisp的新源代码文件,然后在其中键入以下代码。
; this program inputs a numbers and doubles it
(defun DoubleNumber()
(terpri)
(princ "Enter Number : ")
(setq n1 (read))
(setq doubled (* 2.0 n1))
(princ "The Number: ")
(write n1)
(terpri)
(princ "The Number Doubled: ")
(write doubled)
)
(DoubleNumber)
当您执行代码时,它返回以下结果-
Enter Number : 3456.78 (STDIN Input)
The Number: 3456.78
The Number Doubled: 6913.56
函数格式用于生成格式正确的文本。它具有以下语法-
format destination control-string &rest arguments
哪里,
格式指令由波浪号(〜),用逗号分隔的可选前缀参数,可选冒号(:)和符号(@)修饰符以及指示此指令类型的单个字符。
前缀参数通常是整数,用可选的带符号十进制数表示。
下表简要介绍了常用指令-
Sr.No. | Directive & Description |
---|---|
1 |
~A Is followed by ASCII arguments. |
2 |
~S Is followed by S-expressions. |
3 |
~D For decimal arguments. |
4 |
~B For binary arguments. |
5 |
~O For octal arguments. |
6 |
~X For hexadecimal arguments. |
7 |
~C For character arguments. |
8 |
~F For Fixed-format floating-point arguments. |
9 |
~E Exponential floating-point arguments. |
10 |
~$ Dollar and floating point arguments. |
11 |
~% A new line is printed. |
12 |
~* Next argument is ignored. |
13 |
~? Indirection. The next argument must be a string, and the one after it a list. |
让我们重写计算圆面积的程序-
创建一个名为main.lisp的新源代码文件,然后在其中键入以下代码。
(defun AreaOfCircle()
(terpri)
(princ "Enter Radius: ")
(setq radius (read))
(setq area (* 3.1416 radius radius))
(format t "Radius: = ~F~% Area = ~F" radius area)
)
(AreaOfCircle)
当您执行代码时,它返回以下结果-
Enter Radius: 10.234 (STDIN Input)
Radius: = 10.234
Area = 329.03473