user5693773
user5693773

Reputation:

Learning LISP. Why does NIL print out after end of function using if statement

Hi newbie here trying to learn lisp and im confused as to why lisp prints out NIL at the end of my output? Is there a way for it to not print NIL or is my if statement not set correctly.

(defun greater (x) 
  (if (> x 4) 
    (message "number is greater than 4")))

Get the result:

    [2]> (square 10)
    number greater than 4
    NIL

Upvotes: 0

Views: 786

Answers (2)

melpomene
melpomene

Reputation: 85757

That's just the return value of your function. Your REPL (interactive evaluation) displays the result of each expression you enter. The result of calling your greater function is NIL.

Upvotes: 2

Sylwester
Sylwester

Reputation: 48745

All top level forms get printed by the Read-Eval-Print-Loop. Here is how to avoid it:

;;; make a main function
(defun main () 
  ;; all your program top level forms here!
  (values)) ; empty values return no value and the REPL will not print anything when your program terminates

;; call main
(main)

Of course in an interactive session you would like the result printed out so that you can enter (+ 2 3) and get 5 back without having to wrap it in a print statement.

Upvotes: 3

Related Questions