lispcommon-lispclisp

How to stop getting NIL from if statement in LISP


Sorry to ask this question, but I was learning optional parameters in LISP Hence I created a function called "calculate-bill" which will take two parameters :

So as you can guess if the discount is passed to the function it will cut its value from the amount else just print the amount.

Code :

(defun calculate-bill (amount &optional discount)
  (if (null discount)
    (format t "Total bill is ~d." amount))
  (if discount
    (format t "Total bill is ~d." (- amount discount))))

(write (calculate-bill 200 50))
(terpri)
(write (calculate-bill 200))

Here I had added null condition in if blocks.

I want my expected output to be :

Total bill is 150.
Total bill is 200.

But the current output is :

Total bill is 150.NIL
Total bill is 200.NIL

How can I avoid that "NIL" ?


Solution

  • You are doing double output. You call FORMAT and WRITE.

    Now you need to find out whether you really want to do double output and, if not, which function to keep and which not.