Author Topic: Syntax of a Conditional  (Read 7136 times)

0 Members and 1 Guest are viewing this topic.

JohnK

  • Administrator
  • Seagull
  • Posts: 10661
Syntax of a Conditional
« on: April 17, 2006, 09:28:52 AM »
I was thinking a little bit over the weekend about something I did without thinking on Friday. I was drawing a quick plumbing riser and I kept hitting the wrong function key to flip my isometric plain. A small annoyance but I was going to create a new redefinition real quick. So I literally whipped off a couple liner proced that didnt work. I looked at the code I wrote again and I saw something goofy...I wrote the conditional in that small procedure a weird way. HOWEVER, it makes total sense. I'll show you how I initially wrote the conditional statement.

Code: [Select]
(cond (getvar 'snapisopair)
       (1 (setvar 'snapisopair 2))
       (2 (setvar 'snapisopair 0))
       (0 (setvar 'snapisopair 1)) )

I dont know why I wrote it like this, I dont know if I was confusing another language or what but the syntax makes sense to me I guess. Here would be my first shot at explaining it.

(conditional <evaluator>
             '((<predicate> <subsequent>)...(<predicate> <subsequent>)))

Ultimately, If I were to re-write the cond statement it would require me to create a var or evaluate multiple times but...What do [you] think about this syntax (Existing or my goof up)?

Oh BTW, I realize that if I were to spend time re-writing the cond statement, my procedures would be bloated...It just wouldn't be worth it. This was meant as a discussion into the syntax of conditionals, variables and etc.
« Last Edit: April 17, 2006, 09:41:43 AM by Se7en »
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10661
Re: Sytax of a Conditional
« Reply #1 on: April 17, 2006, 09:31:21 AM »
Wouldn't it be cool to ``capture'' the return without creating a varible. (Well on our end I mean, slots are filled no matter what, but I was just thinking that this sytax would make it alot easier to understand. )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Syntax of a cond structure
« Reply #2 on: April 17, 2006, 09:40:54 AM »
An alternative --

Code: [Select]
(setvar "snapisopair"
    (cdr
        (assoc
            (getvar "snapisopair")               
           '((0 . 1)(1 . 2)(2 . 0))
        )
    )
)

PS: Syntax of a cond structure.
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

JohnK

  • Administrator
  • Seagull
  • Posts: 10661
Re: Syntax of a Conditional
« Reply #3 on: April 17, 2006, 09:47:22 AM »
That's very interisting MP! That's a very efficient alternitive to what I would have done. I supose it would have been something like:
Code: [Select]
(if (>= (getvar 'snapisopair) 2)
  (setvar 'snapisopair 0)
  (setvar 'snapisopair (1+ (getvar 'snapisopair)))
  )
hummm...one less `call'. 

That's a very cool way to think about the situation. You are good sir.
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Syntax of a Conditional
« Reply #4 on: April 17, 2006, 10:00:39 AM »
Thanks John.

If I were going to go the incrementing route I'd probably pen a general use utility --

Code: [Select]
(defun NextInt ( currentInt lowerBound upperBound )
    (if (< (1- lowerBound) currentInt upperBound)
        (1+ currentInt)
        lowerbound
    )
)

And then use it --

Code: [Select]
(setvar "SnapIsoPair"
    (NextInt (getvar "SnapIsoPair") 0 2)
)
« Last Edit: April 17, 2006, 10:09:38 AM by MP »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Syntax of a Conditional
« Reply #5 on: April 17, 2006, 10:09:01 AM »
If you don't mind working back wards.
Code: [Select]
(setvar "snapisopair"
        (if (zerop (getvar "snapisopair"))
          2
          (1- (getvar "snapisopair"))
        )
)

else

Code: [Select]
(setvar "snapisopair"
        (if (= (getvar "snapisopair") 2)
          0
          (1+ (getvar "snapisopair"))
        )
)

Youse guys type way too fast :-)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Syntax of a Conditional
« Reply #6 on: April 17, 2006, 10:27:35 AM »
OK, here is another one
Code: [Select]
(setvar "snapisopair"
        (cadr (member (getvar "snapisopair") '(0 1 2 0))
)

And another

Code: [Select]
(setvar "snapisopair"
        (vl-position (getvar "snapisopair") '(2 0 1))
)
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

JohnK

  • Administrator
  • Seagull
  • Posts: 10661
Re: Syntax of a Conditional
« Reply #7 on: April 17, 2006, 10:28:09 AM »
MP, for readability? (I mean i see the value but, i think its more for readability only.)

CAB, nice, but your still `getting' the value twice (In certian cases of course).
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Syntax of a Conditional
« Reply #8 on: April 17, 2006, 10:53:55 AM »
If wer're going for brevity and readability perhaps --

Code: [Select]
(setvar "snapisopair"
    (nth
        (getvar "snapisopair")               
       '(1 2 0)
    )
)

:)
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

whdjr

  • Guest
Re: Syntax of a Conditional
« Reply #9 on: April 17, 2006, 11:28:50 AM »
Man...you guys must have some special lisp goggles or seomething...How do you guys come up with these solutions?  ooo!!  :lol:  my head hurts...I see the original post and I see water with some fish in it and you guys with your lisp goggles see a pond with trout, salmon, perch, bass, pike, ...etc.   :wink:

Very good discussion.  I definitely see things in a different light now. :-)

MP

  • Seagull
  • Posts: 17750
  • Have thousands of dwgs to process? Contact me.
Re: Syntax of a down
« Reply #10 on: April 17, 2006, 11:50:08 AM »
Engineering Technologist • CAD Automation Practitioner
Automation ▸ Design ▸ Drafting ▸ Document Control ▸ Client
cadanalyst@gmail.comhttp://cadanalyst.slack.comhttp://linkedin.com/in/cadanalyst

JohnK

  • Administrator
  • Seagull
  • Posts: 10661
Re: Syntax of a Conditional
« Reply #11 on: April 17, 2006, 11:55:26 AM »
MP, No i prefer the first one over the second. I think the first version's intension is very clear for ALL users. The second version is ``playing with semantics'' so to speak. I mean its very nice but the play on the list order is...i think the point would be missed to easily. But yes, i agree whole heartedly that it is a very nice alternative. I think it may even be faster.

whdjr, No goggles here. Im just playing with evaluation's. (i just asked the question to myself "is a variable really needed in this situation?" All the rest is just by-product.)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Syntax of a Conditional
« Reply #12 on: April 17, 2006, 05:11:08 PM »
Code: [Select]
... I dont know why I wrote it like this, I dont know if I was confusing another language or what ...

almost looks like a case statement structure John.  .. to paraphrase Harold Abelson  .. " sometimes you have to use magic to cross the abstraction barrier  "
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Syntax of a Conditional
« Reply #13 on: April 17, 2006, 11:20:48 PM »
Code: [Select]
... I dont know why I wrote it like this, I dont know if I was confusing another language or what ...

almost looks like a case statement structure John.  .. to paraphrase Harold Abelson  .. " sometimes you have to use magic to cross the abstraction barrier  "

Here 'ya go John ...

< insert comment about where you were when this was written >

Code: [Select]
;; unedited original ...
;;; CASE.LSP  Copyright  1991  Tony Tanzillo  All Rights Reserved
;;;
;;; (case <value> <cases> )
;;;
;;; Emulates the Common LISP (case) function.
;;;
;;; Select expressions to evaluate using a matching key value.
;;;
;;; Expanded syntax:
;;;
;;;    (case <value> '( ( <test value1> <result expr>...)
;;;                     ( <test value2> <result expr>...)
;;;                     ( <test valueN> <result expr>...)
;;;                   )
;;;    )
;;;
;;; <testvalueN> is the selection key for the expressions that proceed it.
;;; It can be an atom or a list.  If <value> is equal to a <testvalue> or
;;; is a member of a <testvalue>, then all expressions which proceed the
;;; matching <testvalue> will in-turn be evaluated, and the result of the
;;; last result expression will be returned by (case).
;;;
;;; The test value of each case matches the <value> argument if it is EQUAL
;;; to <value>, or if the test value is a list which <value> is a MEMBER of.
;;;
;;; If a clause whose test value matches <value> has no proceeding result
;;; expressions, then CASE returns the test value.
;;;
;;; The symbol T can be used as the test value in the last clause to cause
;;; it to act as a 'default' clause, and the result expressions which follow
;;; it will be evaluated if no preceeding test value matches <value>.
;;;
;;; EQUAL tests take precedence over MEMBER tests.  Or, if one test value is
;;; EQUAL to <value> and another test value is a list that <value> is a MEMBER
;;; of, then the result expressions of the test value which is EQUAL to <value>
;;; take precedence and is selected.
;;;
;;; Example:
;;;
;;;    [convert the value of an integer into some string]
;;;
;;;    (setq num 3)
;;;
;;;    (setq digit (case num '(  (1 "one")
;;;                              (2 "two")
;;;                              (3 "three")  ; <- num = 3, case selects this
;;;                              (4 "four")
;;;                              ((5 6 7 8) "Num is > 3 and < 9")
;;;                              (t "Greater than eight.") )))
;;;
;;;
;;;    Returns: "three"

(DEFUN case (value cases / clause)
  (COND ((SETQ clause (ASSOC value cases)) (do-clause clause))
        (T (case-aux cases))
  )
)


(DEFUN case-aux (cases)
  (COND ((NOT cases) nil)
        ((OR (AND (LISTP (CAAR cases)) (MEMBER value (CAAR cases)))
             (AND (NOT (CDR cases)) (EQUAL T (CAAR cases)))
         )
         (do-clause (CAR cases))
        )
        (T (case-aux (CDR cases)))
  )
)




(DEFUN do-clause (clause)
  (let nil
       (COND ((CDR clause))
             (T (LIST (QUOTE (CAR clause))))
       )
  )
)


;;;  (let <bindings> <body> )
;;;
;;;  (let '( [(<sym> <expr>)]...)
;;;       '( <expr>...)
;;;  )
;;;
;;;  Emulates the Common LISP (let) function.
;;;
;;;  Perform evaluation of expressions with local bindings.
;;;
;;;  <bindings> is a list of symbols and associated value expressions
;;;  which produce the values each symbol is assigned within the local
;;;  enviroment of the evaluation of the let <body>.
;;;
;;;  <body> is a list of expressions to be evaluated within the scope
;;;  of the local bindings.
;;;
;;;  Example:
;;;
;;;     Command: (setq x 1)
;;;     1
;;;     Command: (setq y 2)
;;;     2
;;;     Command: (let '((x 10) (y 20))                           ; bindings
;;;                   '(  (princ "\nLocal values of X and Y: ")  ; body
;;;                       (prin1 x)
;;;                       (prin1 y)
;;;                       (princ)
;;;                    )
;;;              )
;;;     Local values of X and Y: 10 20
;;;     20
;;;     Command: (print x)
;;;     1
;;;     Command: (print y)
;;;     2
;;;     Command:

(DEFUN let (bindings body)
  (EVAL (CONS (APPEND (LIST 'LAMBDA (MAPCAR 'CAR bindings)) body)
              (MAPCAR 'CADR bindings)
        )
  )
)
« Last Edit: April 18, 2006, 01:19:14 AM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Syntax of a Conditional
« Reply #14 on: April 17, 2006, 11:26:45 PM »
... so ...
Code: [Select]
(case (GETVAR 'snapisopair)
      '((1 (SETVAR 'snapisopair 2))
        (2 (SETVAR 'snapisopair 0))
        (0 (SETVAR 'snapisopair 1))
       )
)

/// kwb
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Sdoman

  • Guest
Re: Syntax of a Conditional
« Reply #15 on: April 18, 2006, 12:31:41 AM »
Code: [Select]
(cond (getvar 'snapisopair)
       (1 (setvar 'snapisopair 2))
       (2 (setvar 'snapisopair 0))
       (0 (setvar 'snapisopair 1)) )

When I run the code above it returns 'snapisopair, and leaves the value of snapisopair unchanged.  :?  I must be missing something...


 

Jürg Menzi

  • Swamp Rat
  • Posts: 599
  • Oberegg, Switzerland
Re: Syntax of a Conditional
« Reply #16 on: April 18, 2006, 02:58:52 AM »
Code: [Select]
(cond (getvar 'snapisopair)
       (1 (setvar 'snapisopair 2))
       (2 (setvar 'snapisopair 0))
       (0 (setvar 'snapisopair 1)) )

When I run the code above it returns 'snapisopair, and leaves the value of snapisopair unchanged.  :?  I must be missing something... 
Hummmm... case? :roll:
A computer's human touch is its unscrupulousness!
MENZI ENGINEERING GmbH
Current A2k16... A2k24 - Start R2.18

JohnK

  • Administrator
  • Seagull
  • Posts: 10661
Re: Syntax of a Conditional
« Reply #17 on: April 18, 2006, 07:25:50 AM »
Here 'ya go John ...

< insert comment about where you were when this was written >
...

Holly cow!? Jackpot! ...Umm, I would have been in Junior high.

No kidding, i was trying to build a LET yesterday.

Kerry, are you sure you arnt working in my office, cause it seems like you always do this to me; You always seem to know what im doing/thinking/etc.  ...Are you some sort of evil genius?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Sdoman

  • Guest
Re: Syntax of a Conditional
« Reply #18 on: April 18, 2006, 08:00:40 AM »
Hummmm... case? :roll:

OK, I get it know.  I was assuming that in the original code Se7en had posted that he stumbled across some side effect of the Cond function which allowed it to work as a Case function and it actually worked.  That's how I read it.  Apparently it went over my head.   :-)

Anyhow, I like this one from MP:

Code: [Select]
(setvar "snapisopair"
    (nth
        (getvar "snapisopair")               
       '(1 2 0)
    )
)