order of argument evaluation in equal()

Discussion in 'Cadence' started by fogh, Oct 21, 2004.

  1. fogh

    fogh Guest

    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.
     
    fogh, Oct 21, 2004
    #1
  2. 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, Oct 21, 2004
    #2
  3. fogh

    reb Guest

    (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.
     
    reb, Oct 22, 2004
    #3
  4. fogh

    fogh Guest

    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
     
    fogh, Oct 22, 2004
    #4
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.