How to write in short form for conditions

How to write in short form for conditions

avinash00002002
Collaborator Collaborator
229 Views
3 Replies
Message 1 of 4

How to write in short form for conditions

avinash00002002
Collaborator
Collaborator

Hi

 

I have a conditions like below

 

(if (and (= ang 40) (= angt 4))
(setq bmark 20)
)
(if (and (= ang 40) (= angt 5))
(setq bmark 20)
)
(if (and (= ang 45) (= angt 4))
(setq bmark 23)
)
(if (and (= ang 43) (= angt 4))
(setq bmark 23)
)
(if (and (= ang 45) (= angt 5))
(setq bmark 23)
)
(if (and (= ang 45) (= angt 6))
(setq bmark 25)
)
(if (and (= ang 45) (= angt 7))
(progn (setq bmark 23)
(setq angtold angt)
(setq angt 4)
))
(if (and (= ang 45) (= angt 8))
(progn (setq bmark 23)
(setq angtold angt)
(setq angt 5)
))

 

ang = angle section

angt = anglt thickness

bmark = backmark

 

any suggestion.

 

Thanks,

 

Avinash

0 Likes
230 Views
3 Replies
Replies (3)
Message 2 of 4

annoisscary
Advocate
Advocate

I usually go for cond() in these types of situations as you don't need to worry about throwing in a progn when the condition is met and you need to do lots of stuff. Also I feel like it is just a little more readable and a little less code bloat when having do many conditions.

 

(cond
	(
		(and (= ang 40) (= angt 4))
		(setq bmark 20)
	)
	(
		(and (= and 40) (= angt 5))
		(setq bmark 20)
	)
	(
		(and (= ang 45) (= angt 4))
		(setq bmark 23)
	)
	(
		(and (= ang 43) (= angt 4))
		(setq bmark 23)
	)
	(
		(and (= ang 45) (= angt 5))
		(setq bmark 23)
	)
	(
		(and (= ang 45) (= angt 6))
		(setq bmark 25)
	)
	(
		(and (= ang 45) (= angt 7))
		(setq bmark 23)
		(setq angtold angt)
		(setq angt 4)
	)
	(
		(and (= ang 45) (= angt 8))
		(setq bmark 23)
		(setq angtold angt)
		(setq angt 5)
	)
)

 

You could also consider grouping the conditions that will end up with the same result

 

(cond
	(
		(and (= ang 40) (= angt 4))
		(setq bmark 20)
	)
	(
		(and (= and 40) (= angt 5))
		(setq bmark 20)
	)
	(
		(or
			(and (= ang 45) (= angt 4))
			(and (= ang 43) (= angt 4))
			(and (= ang 45) (= angt 5))
		)
		(setq bmark 23)
	)
	(
		(and (= ang 45) (= angt 6))
		(setq bmark 25)
	)
	(
		(and (= ang 45) (= angt 7))
		(setq bmark 23)
		(setq angtold angt)
		(setq angt 4)
	)
	(
		(and (= ang 45) (= angt 8))
		(setq bmark 23)
		(setq angtold angt)
		(setq angt 5)
	)
)

 

Honestly, not 100% certain the syntax of the or is correct right now, its super early and I cant be bothered to test atm.

0 Likes
Message 3 of 4

komondormrex
Mentor
Mentor

hey,

other one

(setq bmark
	(cond
		((or (and (= ang 40) (= angt 4))
			 (and (= ang 40) (= angt 5))
		 )
			20
		)
		((or (and (= ang 45) (= angt 4))
			 (and (= ang 43) (= angt 4))
			 (and (= ang 45) (= angt 5))
			 (and
			 	  (and (= ang 45) (= angt 7))
				  (setq angtold angt)
				  (setq angt 4)
			 )
			 (and
			 	  (and (= ang 45) (= angt 8))
				  (setq angtold angt)
				  (setq angt 5)
			 )
		 )
			23
		)
		((and (= ang 45) (= angt 6))
			25
		)
	)
)
0 Likes
Message 4 of 4

john.uhden
Mentor
Mentor

@annoisscary ,

I really didn't check all those conditions, but your structure looks fine.

Never seen "atm" before but I take it that it means "at this moment" not "automated teller machine."

John F. Uhden

0 Likes