Функции вывода в LISP

Опубликовано: 9 Января, 2023

Функциям вывода требуется необязательный аргумент, известный как поток вывода, который по умолчанию является «потоком вывода» и может быть назначен другому потоку, в который должен быть отправлен вывод.

  • Напишите:

write object   key : stream

Пример 1:

Lisp




; LISP program for Write
(write "Hello World")
(write "To")
(write "The")
(write "LISP")
(write "World")

Выход:

  • печать, печать, принцип, принцип1:

print object   stream

print ->>prints  with a preceding newline and followed by a space.

pprint object stream

pprint ->>like print except that the trailing space is omitted.

princ object stream

princ ->>like prin1 except that the output has no escape character

prin1 object stream

prin1 ->>returns the item as its value.

Пример 2:

Lisp




; LISP program for print
(print "Hello World")
(pprint "To")
(prin1 "The")
(princ "LISP")

Выход:

  • писать в строку, печатать в строку:

write-to-string object  key stream 

prin1-to-string object

princ-to-string object The output character string is returned as object from functions.

  • запись-символ, запись-строка, запись-строка:

write-char character  stream return character and print output

write-string string  stream  key :start :end writes the particular substring of string to output 
write-line string  stream   key :start :end same as write-string but succeed with newline

  • Функции новой строки:

terpri  stream

print a new line to the stream
fresh-line  stream

print new line if stream is not at start of line.

  • написать байт:

write-byte integer binary-output-stream

write one byte i.e. the value of integer.

  • завершить вывод, принудительно вывести, очистить вывод:

finish-output & optional output-stream

ensure all output sent is reached to stream and then only return null. 

force-output & optional output-stream

return nil without waiting for completion of task and force output start the emptying of buffer

clear-output & optional output-stream

abort any outstanding output operation in progress

  • вывод в бинарный поток:

write-byte integer binary-output-stream

write-byte writes one byte, the value of integer. It is an error if integer is not of the type specified as the :element-type argument to open when the stream was created. The value integer is returned.

  • вывод в потоки символов:

write object &key :stream :escape :radix :base :circle :pretty 

:level :length :case :gensym :array :readably 

:right-margin :miser-width :lines :pprint-dispatch

same as above the output stream can be specified for other functions like as:

prin1 object &optional output-stream
print object &optional output-stream
pprint object &optional output-stream
princ object &optional output-stream

write-to-string object &key 

:escape :radix :base :circle :pretty :level :length :case :gensym :array
prin1-to-string object
princ-to-string object

write-char character &optional output-stream

terpri &optional output-stream
fresh-line &optional output-stream

finish-output &optional output-stream
force-output &optional output-stream
clear-output &optional output-stream

Пример 3:

Lisp




; this program inputs a number and divide it by 2
(defun QuadNumber()
   (terpri) 
    
  ;for new line
   (princ "Input Number : "
    
  ;print statement "Input Number :"
   (setq n1 (read))          
    
  ;set n1 to input number 
   (setq quad (/ n1 4.0)) 
    
  ;set n1=n1/4
   (princ "The Number: ")
   (write n1)                 
    
  ;write n1
   (terpri)                     
    
  ;new line
   (princ "The Number"s fourth part: ")
   (write quad)                 
   
  ;write quad=n1/4 
 )
 (QuadNumber)                 ;
 call function DoubleNumber

Выход: