lisp princ详解_LISP-输入和输出

LISP-输入和输出

常见的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.

从键盘读取输入

读取函数用于从键盘获取输入。可能没有任何论据。

例如,考虑代码片段-

假设用户从STDIN输入中输入10.2,它将返回,

read函数从输入流中读取字符,并通过解析为Lisp对象的表示来解释它们。

创建一个名为main.lisp的新源代码文件,并在其中键入以下代码-

当您执行代码时,它返回以下结果-

创建一个名为main.lisp的新源代码文件,然后在其中键入以下代码。

当您执行代码时,它返回以下结果-

输出功能

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 −

prin1 returns the object as its value.

print prints the object with a preceding newline and followed by a space. It returns object.

pprint is just like print except that the trailing space is omitted.

princ is just like prin1 except that the output has no escape character

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

The function finish-output attempts to ensure that all output sent to output-stream has reached its destination, and only then returns nil.

The function force-output initiates the emptying of any internal buffers but returns nil without waiting for completion or acknowledgment.

The function clear-output attempts to abort any outstanding output operation in progress in order to allow as little output as possible to continue to the destination.

10

write-byte integer binary-output-stream

It writes one byte, the value of the integer.

创建一个名为main.lisp的新源代码文件,然后在其中键入以下代码。

当您执行代码时,它返回以下结果-

格式化输出

函数格式用于生成格式正确的文本。它具有以下语法-

哪里,

目标是标准输出

控制字符串保存要输出的字符和打印指令。

格式指令由波浪号(〜),用逗号分隔的可选前缀参数,可选冒号(:)和符号(@)修饰符以及指示此指令类型的单个字符。

前缀参数通常是整数,用可选的带符号十进制数表示。

下表简要介绍了常用指令-

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的新源代码文件,然后在其中键入以下代码。

当您执行代码时,它返回以下结果-

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值