Jump to content

Need help spotting error in short lisp


MechanicalAnimal

Recommended Posts

Hey all,

 

I am new to writing lisps and wondered if someone could help me spot the error in this code. All I want to do is have the user enter in two primary colors and then have the lisp reveal the secondary color that is made when mixing the two.

 

For some reason I keep getting an error "malformed list on input" when I load it into CAD.

 

 

If someone could help me spot the issue I would really appreciate it!!! ( I have a feeling it is something with the cond expression and/or a parenthesis.

 

(defun c:rainbow()
  (setq c1 "red")   ;set red to c1
  (setq c2 "blue")  ;set blue to c2
  (setq c3 "yellow");set yellow to c3
 
  (setq str1 (getstring T "\n Enter first Primary Color. (Red, Blue, or Yellow).")) ; first primary color entered and assigned to str1
  (setq str2 (getstring T "\n Enter second Primary Color. (Red, Blue, or Yellow).")) ; first primary color entered and assigned to str1
     (cond ((and (= str1 c1) (= str2 c1) 
  (princ (strcat "\nWhen mixing " c1 "and " c1 " you will get RED as a final color."))
  )
  ((and (= str1 c1) (= str2 c2)
  (princ (strcat "\nWhen mixing " c1 "and " c2 " you will get PURPLE as a final color."))
  )       
  ((and (= str1 c1) (= str2 c3) 
  (princ (strcat "\nWhen mixing " c1 "and " c3 " you will get ORANGE as a final color."))
  )
  ((and (= str1 c2) (= str2 c1)
  (princ (strcat "\nWhen mixing " c2 "and " c1 " you will get PURPLE as a final color."))
  )
  ((and (= str1 c2) (= str2 c2)
  (princ (strcat "\nWhen mixing " c2 "and " c2 " you will get BLUE as a final color."))
  )
  ((and (= str1 c2) (= str2 c3)
   (princ (strcat "\nWhen mixing " c2 "and " c3 " you will get GREEN as a final color."))
  )
  ((and (= str1 c3) (= str2 c1)
  (princ (strcat "\nWhen mixing " c3 "and " c1 " you will get ORANGE as a final color."))
  )
  ((and (= str1 c3) (= str2 c2)
  (princ (strcat "\nWhen mixing " c3 "and " c2 " you will get GREEN as a final color."))
  )
  ((and (= str1 c3) (= str2 c3)
  (princ (strcat "\nWhen mixing " c3 "and " c2 " you will get YELLOW as a final color."))
  )
    )
(princ)
)

Edited by rkmcswain
Added [CODE] tags
Link to comment
Share on other sites

See if this reference helps you to detect the problem ;)

 

PS: Please edit your post and enclose your code with code tags:

 

[highlight][noparse]

[/noparse][/highlight] Your code here [highlight][noparse]

[/noparse][/highlight]

Link to comment
Share on other sites

When dealing with such combinations, you may find it easier to work with bit-codes, as this will account for both a+b & b+a simultaneously, e.g.:

(defun c:rainbow ( / a b col mix )
   (setq col '(("Red" . 1) ("Blue" . 2) ("Yellow" . 4))
         mix '((1 . "Red") (2 . "Blue") (3 . "Purple") (4 . "Yellow") (5 . "Orange") (6 . "Green"))
   )
   (initget 1 "Red Blue Yellow")
   (setq a (getkword "\nEnter 1st colour [Red/Blue/Yellow]: "))
   (initget 1 "Red Blue Yellow")
   (setq b (getkword "\nEnter 2nd colour [Red/Blue/Yellow]: "))
   (princ
       (strcat "\nWhen mixing " a " and " b " you will get "
           (cdr (assoc (logior (cdr (assoc a col)) (cdr (assoc b col))) mix))
       )
   )
   (princ)
)

Link to comment
Share on other sites

LIsten to Lee, he's smart. But also, don't forget to close your "and" statements ie

(and (= str1 c1) (= str2 c2))

Link to comment
Share on other sites

So I consider myself still in the "learning" phase of building autolisps and as I am sure there are far better ways to lay this program out structurally, I kind of just want to know where I went wrong when programming it the way I have it laid out.

This way I can learn from my mistakes.

 

 

I keep going through this program thinking that what I have laid out should work however I am getting the error "malformed list on input" which I found could mean an error on my parenthesis, however I am unable to find a mismatched pair.

Have I declared one of my variables using the wrong terminology?

Is there anything else that this error code could mean?

 

 

I apologize for any inconvenience but I know that I am either missing something very small or that I am using some of the call outs wrong.

 

 

Once again any help would be greatly appreciated!

 

 

 

 

(defun c:rainbowtest()
  (setq c1 "Red")   ;set red to c1
  (setq c2 "Blue")  ;set blue to c2
  (setq c3 "Yellow");set yellow to c3
 
(setq str1 (getstring T "\n Enter first Primary Color. (Red, Blue, or Yellow).")) ; first primary color entered and assigned to str1
  (setq str2 (getstring T "\n Enter second Primary Color. (Red, Blue, or Yellow).")) ; second primary color entered and assigned to str1
     (cond
     (and (= str1 c1) (= str2 c1)
  (princ (strcat "\nWhen mixing "C1" and "C1" you will get RED as a final color.")
  )
     (and (= str1 c1) (= str2 c2)
  (princ (strcat "\nWhen mixing "C1" and "C2" you will get PURPLE as a final color.")
  ) 
     (and (= str1 c1) (= str2 c3) 
  (princ (strcat "\nWhen mixing "C1" and "C3" you will get ORANGE as a final color.")
  )
     (and (= str1 c2) (= str2 c1)
  (princ (strcat "\nWhen mixing "C2" and "C1" you will get PURPLE as a final color.")
  )
     (and (= str1 c2) (= str2 c2)
  )  
  (princ (strcat "\nWhen mixing "C2" and "C2" you will get BLUE as a final color.")
  )
     (and (= str1 c2) (= str2 c3)
   (princ (strcat "\nWhen mixing "C2" and "C3" you will get GREEN as a final color.")
  )
     (and (= str1 c3) (= str2 c1)
  (princ (strcat "\nWhen mixing "C3" and "C1" you will get ORANGE as a final color.")
  )
     (and (= str1 c3) (= str2 c2)
  (princ (strcat "\nWhen mixing "C3" and "C2" you will get GREEN as a final color.")
  )
     (and (= str1 c3) (= str2 c3)
  (princ (strcat "\nWhen mixing "C3" and "C3" you will get YELLOW as a final color.")
         )   
   )
)

Link to comment
Share on other sites

Hint:

(cond
   [highlight]([/highlight]   (and (= str c1) (= str2 c1)[highlight])[/highlight]
       (princ (strcat "\nWhen mixing "C1" and "C1" you will get RED as a final color."))
   [highlight])[/highlight]
   ...
)

 

What code editor are you using to write your code?

Link to comment
Share on other sites

Ok so I made the 3 paren change to each and portion of the program, saved it.

 

 

The program is running!!!

 

It prompts me to enter both colors, however then it just returns "nil".

 

 

I believe this has to do with case sensitivity because if I enter "red" "blue" or "yellow" as my answer I get nil.

 

However if I enter "Red", "Blue" or "Yellow" as my answer the program works.

 

So when set my variables at the top of my program, how can I set c1 to equal "Red" and or "red", and the same with blue and yellow.

 

 

This way no matter what the user types in it will work.

 

 

Thanks for everything Lee Mac and everyone else who helped out!

Link to comment
Share on other sites

I am typing the code directly into the visual lip editor.

 

Good to hear - you should therefore see the corresponding open parenthesis flash briefly when you type a closing parenthesis, making it easier to match the parentheses surrounding each expression.

 

With consistent indentation, I find it relatively easy to match parentheses in AutoLISP, however when in doubt, you can also double-click beside the parenthesis in the VLIDE to quickly view all code enclosed by the parenthesis, e.g.:

 

vlidematchparens.gif

 

So when set my variables at the top of my program, how can I set c1 to equal "Red" and or "red", and the same with blue and yellow.

 

This way no matter what the user types in it will work.

 

I would suggest using keywords, as shown in my example.

Link to comment
Share on other sites

I believe this has to do with case sensitivity because if I enter "red" "blue" or "yellow" as my answer I get nil.

 

However if I enter "Red", "Blue" or "Yellow" as my answer the program works.

 

So when set my variables at the top of my program, how can I set c1 to equal "Red" and or "red", and the same with blue and yellow.

 

I agree with Lee Mac, but if you don't want to take that route you can always use "STRCASE" ie:

   (setq c1 "RED")   ;set red to c1
  (setq c2 "BLUE")  ;set blue to c2
  (setq c3 "YELLOW");set yellow to c3

  (setq str1 (getstring T "\n Enter first Primary Color. (Red, Blue, or Yellow).")) ; first primary color entered and assigned to str1
  (setq str2 (getstring T "\n Enter second Primary Color. (Red, Blue, or Yellow).")) ; first primary color entered and assigned to str1
(setq str1 (strcase str1)
       str2 (strcase str2))

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...