order of argument evaluation in equal()
CADForums.net Forum Index CADForums.net
Discussion of AutoCAD and other CAD software.
 
 FAQFAQ   MemberlistMemberlist     RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
 
Google
 
Web cadforums.net
order of argument evaluation in equal()

 
Post new topic   Reply to topic    CADForums.net Forum Index -> Cadence
Author Message
fogh
Guest





Posted: Thu Oct 21, 2004 2:04 pm    Post subject: order of argument evaluation in equal() Reply with quote

Is it safe to use this syntax
unless(x==(x=SomeNewValue()) info("we have a brand new x"))
This spares me the definition of an xOld variable but feels a bit risky.

Back to top
David Cuthbert
Guest





Posted: Thu Oct 21, 2004 6:37 pm    Post subject: Re: order of argument evaluation in equal() Reply with quote

fogh wrote:
Quote:
Is it safe to use this syntax
unless(x==(x=SomeNewValue()) info("we have a brand new x"))
This spares me the definition of an xOld variable but feels a bit risky.

Although it works (today), it is neither guaranteed to work nor is it
good style (IMHO).

The order of argument evaluation in SKILL (and most languages) is not
documented -- intentionally. Constraining the order of argument
evaluation reduces the ability to optimise the resulting code. SKILL
doesn't do anything like this today, but it could in the future.

The style is suspect because most people expect:
a == b
and
b == a
to mean the same thing.

Perhaps a defining and using a macro to detect changes would better
suffice? It certainly conveys the intent better:

(defmacro changedp (condition @rest body)
`(let ( (__prevalue ,condition) )
,@body
(nequal __prevalue ,condition)))

when(changedp(x x=SomeNewValue())
info("we have a brand new x"))

--
David Cuthbert (dacut@cadence.com) Tel: (412) 599-1820
Cadence Design Systems R&D
Back to top
Guest






Posted: Fri Oct 22, 2004 12:20 pm    Post subject: Re: order of argument evaluation in equal() Reply with quote

fogh <cad_support@skipthisandunderscores.catena.nl> wrote in message news:<4177899f$0$30037$e4fe514c@dreader13.news.xs4all.nl>...
Quote:
Is it safe to use this syntax
unless(x==(x=SomeNewValue()) info("we have a brand new x"))
This spares me the definition of an xOld variable but feels a bit risky.

(defmacro equalOrdered (first second)
`(apply 'equal (mapcar 'eval '(,first ,second))))

Another variety. Doing the eval's yourself to guarantee the order.
mapcar is useful for that. Sometimes (prog1 x x=...) is also useful
for returning a previous value for more operations on it. (When
writing such a macro, just like cpp macros in C, beware of triggering
evaluations of the arguments more than once, since as here they can
have side effects).

Still, unless(equalOrdered(x x=SomeNewValue())) ... is likely less
obvious to most folks than using the extra variable:

(let ((nextx SomeNewValue()))
(unless x == nextx
x = nextx
info(...)))

Some lisp's do document that arguments are evaluated left to right
(apparently not skill, though is there really any specific disclaimer
in the doc's not to rely on ordering?). Would be nice if list() at
least guaranteed (documented) left to right evaluation.

Back to top
fogh
Guest





Posted: Fri Oct 22, 2004 9:43 pm    Post subject: Re: order of argument evaluation in equal() Reply with quote

Reb, Dave,

So, the order is guarantied left-to-right in macros unquotes and in the second to last arguments of let , prog and such places where one expects a chronology of side effects. Nowhere else ?

Too bad. I found this syntax to be rather intuitive. But Dave has a point that some people write the test like
constant == expression
even if I would be more inclined to write it
expression == constant


reb@cypress.com wrote:
Quote:
fogh <cad_support@skipthisandunderscores.catena.nl> wrote in message news:<4177899f$0$30037$e4fe514c@dreader13.news.xs4all.nl>...

Is it safe to use this syntax
unless(x==(x=SomeNewValue()) info("we have a brand new x"))
This spares me the definition of an xOld variable but feels a bit risky.


(defmacro equalOrdered (first second)
`(apply 'equal (mapcar 'eval '(,first ,second))))

Another variety. Doing the eval's yourself to guarantee the order.
mapcar is useful for that. Sometimes (prog1 x x=...) is also useful
for returning a previous value for more operations on it. (When
writing such a macro, just like cpp macros in C, beware of triggering
evaluations of the arguments more than once, since as here they can
have side effects).

Still, unless(equalOrdered(x x=SomeNewValue())) ... is likely less
obvious to most folks than using the extra variable:

(let ((nextx SomeNewValue()))
(unless x == nextx
x = nextx
info(...)))

Some lisp's do document that arguments are evaluated left to right
(apparently not skill, though is there really any specific disclaimer
in the doc's not to rely on ordering?). Would be nice if list() at
least guaranteed (documented) left to right evaluation.
Back to top
 
Post new topic   Reply to topic    CADForums.net Forum Index -> Cadence All times are GMT
Page 1 of 1

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum




Windows Server DSP VoIP Electronics New Topics
Powered by phpBB