How to print a blank character in a Lisp format function?

Hope someone helps me there, because I cannot find a useful answer, and I am new to Lisp.

What I'm trying to do is check the value of one element and print something if it is 1, otherwise to print an empty character. This works when all the arguments to the list have a value of 1:

(defun print-lst (list)
  (format t "~%~a ~a ~a~%"
          (if (= (nth 0 list) '1)
              '¦)
          (if (= (nth 1 list) '1)
              'P)
          (if (= (nth 2 list) '1)
              '¦)))

so the way out ¦ P ¦. But, if the second element in the list is 0, it prints NIL in this place ¦ NIL ¦, and I want it to print a space instead ¦ ¦(and not just to skip this character ¦¦, it is important that the empty character in this position in the output line if the tested value not equal to 1).

Is there a way to return a null character if the condition is (if (= (nth 1 list) '1) 'P)not met or is there another way to fulfill this? Hope I explained it beautifully. Thank.

+3
source share
4 answers

Ifaccepts three arguments: condition, then-form, else-form; else-form is optional. Also, I would use alphabetic character syntax for alphabetic characters.

(if (= (nth 0 list) 1)
    #\P
    #\Space)

Documentation:

+5
source

If you want to make full use of power format, you can use a combination of conditional expressions of the format and the GOTO format.

Like this:

[1]> (format nil "~@{~:[<nothing>~;~:*~A~]~^ ~}" 1 2 nil 4 nil 6)
"1 2 <nothing> 4 <nothing> 6"

In your case, this should work:

(format t "~&~@{~:[ ~;~:*~A~]~^ ~}"
        ...)

This works by doing the following:

  • ~& inserts a new line if we are no longer at the beginning of the line.
  • ~@{...~} .
  • ~:[...~;...~] nil non nil.
  • ~:* , ~:[...~].
  • ~A .
  • ~^ ( ).
+5

:
, ( "-" ), ( "-" )?

: , . : "|", "P" ""

0

- , .

However, it looks like you can use some of your higher-level directives. Check out Formatted output in the Lisp Hyperspec section and format in the PCL chapter (in particular, see the section on conditional formatting).

0
source

All Articles