Open In App

Output Functions in LISP

Improve
Improve
Like Article
Like
Save
Share
Report

Output functions need an optional argument known as output stream which by default is “output-stream” and can be assigned to another stream where the output has to be sent.

  • Write:

write object   key : stream

Example 1:

Lisp




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


Output:

 

  • print, pprint , princ , prin1:

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.

Example 2:

Lisp




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


Output:

 

  • write to string , print to string:

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, write-string, write-line:

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

  • Newline functions:

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:

write-byte integer binary-output-stream

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

  • finish output, force output, clear output:

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

  • output to a binary stream:

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.

  • output to character streams:

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

Example 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


Output:

 



Last Updated : 08 Aug, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads