Common Lisp提供了大量的輸入輸出功能。我們已經(jīng)使用的格式功能,打印輸出功能。在本節(jié)中,我們將探討一些在LISP提供了最常用的輸入輸出功能。
下表提供了LISP的最常用的輸入功能:
| SL No. | 函數(shù)和說明 |
|---|---|
| 1 |
read &optional input-stream eof-error-p eof-value recursive-p
它讀取一個Lisp對象從輸入流的打印形式,建立相應(yīng)的Lisp對象,并返回該對象。 |
| 2 |
read-preserving-whitespace &optional in-stream eof-error-p eof-value recursive-p
這是用在一些特殊情況下,最好是確定擴(kuò)展令牌正好是字符結(jié)束。 |
| 3 |
read-line &optional input-stream eof-error-p eof-value recursive-p
它讀取一個文本行由換行符終止。 |
| 4 |
read-char &optional input-stream eof-error-p eof-value recursive-p
這需要一個字符從輸入流并將其作為一個字符的對象。 |
| 5 |
unread-char character &optional input-stream
它把最近從輸入流中讀取的字符,到輸入數(shù)據(jù)流的前部。 |
| 6 |
peek-char &optional peek-type input-stream eof-error-p eof-value recursive-p
它返回的下一個字符被從輸入流中讀取,而無需實(shí)際從輸入流中除去它。 |
| 7 |
listen &optional input-stream
謂詞監(jiān)聽為true如果有立即從輸入流中的字符,如果不是則為false。 |
| 8 |
read-char-no-hang &optional input-stream eof-error-p eof-value recursive-p
它類似于read-char字符,但是如果它沒有得到一個字符,它不會等待一個字符,但立即返回為nil。 |
| 9 |
clear-input &optional input-stream
它清除與輸入流關(guān)聯(lián)的所有緩沖的輸入。 |
| 10 |
read-from-string string &optional eof-error-p eof-value &key :start :end :preserve-whitespace
它采用字符串的字符,并相繼建立一個LISP的對象,并返回該對象。它也返回第一個字符的索引無法讀取字符串或字符串(或長度+1)的長度,視具體情況而定。 |
| 11 |
parse-integer string &key :start :end :radix :junk-allowed
它會檢查字符串的子串被分隔:start 和:end(默認(rèn)為字符串的開頭和結(jié)尾)。它會跳過空白字符,然后嘗試解析一個整數(shù)。 |
| 12 |
read-byte binary-input-stream &optional eof-error-p eof-value
它讀取1字節(jié)的二進(jìn)制輸入流并將其返回一個整數(shù)的形式。 |
read 函數(shù)用于從鍵盤輸入。也可以不帶任何參數(shù)。
例如,考慮代碼片段:
(write ( + 15.0 (read)))
假設(shè)用戶輸入10.2 來自stdin 輸入,它返回,
25.2
read 函數(shù)從輸入流中讀取字符,并通過解析為Lisp對象的表示解釋它們。
示例
創(chuàng)建一個名為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)
當(dāng)執(zhí)行代碼,它返回以下結(jié)果:
Enter Radius: 5 (STDIN Input) Area: 78.53999
示例
創(chuàng)建一個名為main.lisp一個新的源代碼文件,并在其中輸入如下代碼:
(with-input-from-string (stream "Welcome to Tutorials Yiibai!") (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))
當(dāng)執(zhí)行代碼,它返回以下結(jié)果:
#W #e #l #c #o #m #e #Space # #o #Space
在LISP所有的輸出函數(shù)都有一個稱為輸出流可選參數(shù),其輸出傳送。如果沒有提及或nil,輸出流默認(rèn)為變量*標(biāo)準(zhǔn)輸出*的值。
下表提供了LISP的最常用的輸出函數(shù):
| SL No. | 函數(shù)和說明 |
|---|---|
| 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 既寫對象通過指定的輸出流:stream,默認(rèn)為標(biāo)準(zhǔn)輸出*值*。其他值默認(rèn)為打印設(shè)置相應(yīng)的全局變量。 |
| 2 |
prin1object &optional output-stream print object &optional output-stream pprint object &optional output-stream princ object &optional output-stream 所有這些函數(shù)對象的打印形式輸出到輸出流。但是,下面的不同之處有:
|
| 3 |
write-to-string object &key :escape :radix :base :circle :pretty :level :length :case :gensym :array write-to-stringobject &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 該對象被有效地打印和輸出的字符被轉(zhuǎn)成一個字符串,并將該字符串返回。 |
| 4 |
write-char character &optional output-stream 它輸出的字符輸出流,并返回字符。 |
| 5 |
write-string string &optional output-stream &key :start :end 它寫入字符串的指定子字符串的字符輸出流。 |
| 6 |
write-line string &optional output-stream &key :start :end 它的工作原理與write-string的方式相同,但是之后輸出一個換行符。 |
| 7 |
terpri &optional output-stream 它輸出一個換行符到output-stream。 |
| 8 |
fresh-line &optional output-stream 它只輸出一個換行,如果流不是已經(jīng)在一行的開始。 |
| 9 |
finish-output &optional output-stream force-output &optional output-stream clear-output &optional output-stream
|
| 10 |
write-byte integer binary-output-stream 它寫入一個字節(jié),整數(shù)的值。 |
示例
創(chuàng)建一個名為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)
當(dāng)執(zhí)行代碼,它返回以下結(jié)果:
Enter Number : 3456.78 上一篇:LISP - 概述介紹下一篇:LISP - 函數(shù)