Jump to content

Princ prints twice - Not sure why


CivilTechSource

Recommended Posts

Hi Everyone, I have this piece of lisp that I have written and for some reason it prints twice the result following the if checks. Any ideas why?

 

  (if (< PipeClearance 0)
    (progn
      (command "color" "10")
      (command "circle" intpt 0.5)
      (princ "\nYou messed up! Pipe Clash! Pipe Clash!")
    )
  (if (and (> PipeClearance 0) (< PipeClearance Clearance))
    (progn
    (command "color" "50") 
    (command "circle" intpt 0.5)
    (princ "\nOhhhh Pipes Very Close")
    )
    
    (progn
    (command "color" "80")
    (command "circle" intpt 0.5)
    (princ "\nAll Pipes are Clear")
    )
  )
  ;----
  )

 

Link to comment
Share on other sites

Hi @CivilTechSource,

 

You need to add a (princ) (choose one of these):

- the first one you can add (princ) inside of each "progn" after (princ "\nOhhhh.....) and (princ "\nAll.....)

- the second one you can add (princ) after "if" statement, before or after ";----" in your code.

Adding a (princ) you will get a "clean" output in command line.

Edited by Saxlle
  • Like 2
Link to comment
Share on other sites

@CivilTechSource you dont need to start a new topic this request can be added to your other post.

 

Admin please merge into other post. 

 

Just  a comment as your using multi if's it is probably better to start coding using a COND. 

  • Like 2
Link to comment
Share on other sites

Following Saxlles answer,  it is common to add a (princ) as the last line of the LISP (before the closing ')' ) as "exit silently".

 

The LISP will report the last thing it does, so if the last thing it does is print in the command line "Oooh, pipes are very close" it will repeat that as a report - effectively printing it twice. If the last thing it is asked to do is (princ), it will still repeat it as a report... but since there is no change to the screen it is 'exit quietly'. You can use this report say to run a sub function using the value of the report in a main function to do other stuff - quite useful.

 

 

 

Like the princ lines you use, always space for some more sarcasm in LISPs

 

Link to comment
Share on other sites

5 minutes ago, Steven P said:

The LISP will report the last thing it does, so if the last thing it does is print in the command line "Oooh, pipes are very close" it will repeat that as a report - effectively printing it twice. If the last thing it is asked to do is (princ), it will still repeat it as a report... but since there is no change to the screen it is 'exit quietly'. You can use this report say to run a sub function using the value of the report in a main function to do other stuff - quite useful.

 

Note that the 'report' that you are describing is merely the value returned by the (defun) expression - per the documentation:
 

Quote

Return Values

The result of the last expression evaluated.


This is no different to typing (+ 1 1) at the command line and observing 2 as the return value of the + function; that is, there is nothing special about running a custom command and seeing a return value versus evaluating an arbitrary LISP expression and seeing a return value.

 

Given this, the documentation for the (princ) function reveals why two outputs are observed:
 

Quote

Return Values

The value of the evaluated expr. If called with no arguments, princ returns a null symbol.

 

Hence, when typing (princ "Hello World!") to the command line, you'll obtain:

Command: (princ "Hello World!")
Hello World!"Hello World!"

 

The first being the operation (aka side effect) of the (princ) function, the second being the value returned by the (princ) function.

 

Now, given that (defun) returns the value returned by the last evaluated expression, and (princ) returns the value of the evaluated expression, if you define a function such as:

(defun c:test ( )
    (princ "Hello World!")
)

 

You will obtain the following output:

Command: TEST
Hello World!"Hello World!"

 

Since (princ) has printed the string to the command line and returned "Hello World!" to the (defun) expression, and the (defun) expression has in turn returned "Hello World!" as its own return value.

 

Therefore, by modifying the function definition to:

(defun c:test ( )
    (princ "Hello World!")
    (princ)
)

 

The first (princ) expression still returns "Hello World!" to the (defun) expression, but it is no longer the last evaluated expression - the second (princ) with no arguments will return a null symbol to the (defun) expression, which will then be returned by the (defun) expression, resulting in no visible output to the command line.

  • Like 1
Link to comment
Share on other sites

As a lazy man, if the text to be printed is the result of an operation, I don't use PRINC at all. Let's say I want to print the value of X.

(defun c:pp()
..............
..............
(setq x ....
(strcat "X=" (rtos x))
)

Here STRCAT is the last operation and its result is returned by the PP command. So it will print the result on the command line (only once of ours).

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...