TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: curmudgeon on April 19, 2010, 06:07:28 PM

Title: SET & SETQ alla Kenny
Post by: curmudgeon on April 19, 2010, 06:07:28 PM
Trying to get my head around what Kenny had at Afralisp for SET & SETQ. Here is his code with the comments removed.

Code: [Select]
(defun c:test ( / j lpt symb)
        (setq j 1)
        (while (set     (read (strcat "point_" (itoa j)))
                                                (getpoint "\nPoint : ")
                                )
                (setq j (1+ j))
        )
        (setq j (1- j))
        (sortpoints j)
        (setq lpt (list ""))
        (while (> j 0)
                (setq symb (read (strcat "point_" (itoa j)))
      lpt  (cons (eval symb) lpt)
)
          (set symb nil)
                (setq j (1- j))
        )
        (command "_line")
        (mapcar 'command lpt)
        (princ)
)

(defun sortpoints (j / i cur_y prev_y cur_symb prev_symb point)
        (setq i 2)
        (while (<= i j)
                (setq cur_symb (read (strcat "point_" (itoa i)))
      prev_symb (read (strcat "point_" (itoa (1- i))))
      cur_y (cadr (eval cur_symb))
      prev_y (cadr (eval prev_symb))
)
                (if (> cur_y prev_y)
                        (progn (setq   point (eval prev_symb))
                                (set    prev_symb (eval cur_symb)) 
                                (set    cur_symb point)
                                (if (/= i 2) (setq i (1- i)))
                        )
                        (setq i (1+ i))
                )
        )
)

I do not see any point in the command (set symb nil). What am I missing?
 :ugly:
Title: Re: SET & SETQ alla Kenny
Post by: CAB on April 19, 2010, 08:10:10 PM
The function sortpoints creates variables POINT_1 POINT_2 POINT_3 etc
Then this:
Code: [Select]
        (while (> j 0)
                (setq symb (read (strcat "point_" (itoa j)))
      lpt  (cons (eval symb) lpt)
)
          (set symb nil)
                (setq j (1- j))
        )
        (command "_line")
        (mapcar 'command lpt)
Takes the values from these variables & puts the values in a list.
The variable is no longer needed and because it is global they are set to nil.
It's the same as
(setq POINT_1 nil)
and then
(setq POINT_2 nil)

The actual order is reversed, i.e. counting down but you get the point don't you.

(sorry for the pun, couldn't resist)   8-)
Title: Re: SET & SETQ alla Kenny
Post by: David Bethel on April 20, 2010, 07:48:18 AM
I use this as an example

Code: [Select]
;;;HOPEFULLY THERE IS AN ENTLAST TO GET
   (setq ed (entget (entlast)))

;;;THE LONG WINDED WAY - SETQ
   (setq p10 (cdr (assoc 10 ed)))
   (setq p11 (cdr (assoc 11 ed)))
   (setq p12 (cdr (assoc 12 ed)))
   (setq p13 (cdr (assoc 13 ed)))

;;;A SHORT CRYPTIC WAY - SET
   (foreach g '(10 11 12 13)
      (set (read (strcat "P" (itoa g))) (cdr (assoc g ed))))

The q in setq simply requires the variable named to be supplied in a quoted format
Code: [Select]
(set (quote p10) (cdr (assoc 10 ed)))
(set 'p10 (cdr (assoc 10 ed)))

are also viable

If you use the (set) format, declaring the variable local might lead to debugging errors with something like 'P10 has not been defined'.  So you could  declare it nil at the end of a routine.  -David

Title: Re: SET & SETQ alla Kenny
Post by: Lee Mac on April 20, 2010, 08:19:30 AM
Great explanation David  :-)
Title: Re: SET & SETQ alla Kenny
Post by: CAB on April 20, 2010, 09:25:50 AM
Much better example David 8-)
Title: Re: SET & SETQ alla Kenny
Post by: David Bethel on April 20, 2010, 10:18:10 AM
Thanks. 

I use this kind of thing for dealing with circle points :

Code: [Select]
  (initget 7)
  (setq rad (getdist "\nCircle Radius:   "))

  (initget 6)
  (setq seg (getint "\nCircle Segmemts <16>:   "))
  (or seg (setq seg 16))

  (setq ang 0
        inc (/ (* pi 2) seg)
          p 0)

  (repeat seg
    (set (read (strcat "P" (itoa p)))
         (polar '(0 0 0) ang rad))
    (setq ang (+ ang inc)
          p (1+ p)))


But the global variable problem is there.  -David
Title: Re: SET & SETQ alla Kenny
Post by: CAB on April 20, 2010, 10:39:14 AM
Another good example but I must say, and not talking to you, in Lisp the LIST is the preferred tool.
So gather your variable length data in a list and use length and nth to access the data.
I can't remember a case for me that the 'on the fly' creation of variable names was needed.
Maybe some one can come up with an example where the list can not be used or the variable name is preferred.
Title: Re: SET & SETQ alla Kenny
Post by: Lee Mac on April 20, 2010, 10:53:35 AM
I use set like this sometimes:

Code: [Select]
(mapcar (function set) '(x y z) <point>)

Or with other lists of data.

Title: Re: SET & SETQ alla Kenny
Post by: David Bethel on April 20, 2010, 11:03:47 AM
Actually I use it a lot.  A quick search found 224 lsp routines on my cad machine that uses (set (read


A scenario I use a lot:

DCL file has 6 radio buttons and I need create an output list of only those tiles that are set ("1").  The list can be dynamic.

Code: [Select]


(cond ((< input 60)
          (setq tiles '("VAR1" "VAR2" "VAR3"
                          "WID24" "WID36" "WID48")))
         ((< input 48)
          (setq tiles '("VAR1" "VAR2"
                          "WID24" "WID36"))))

 (foreach n tiles
  (set (read n)
       (get_tile n)))

 (foreach n tiles (if (= (eval (read n)) "1")
                      (setq output (cons n output))))

-David
Title: Re: SET & SETQ alla Kenny
Post by: JohnK on April 20, 2010, 12:51:35 PM
Just posting for posting sake (Im trying to get my numbers up :D ).

Here is two offered as `food fer thought'.

1.) create variables more hygienically (as mentioned in On-Lisp).
2.) cause the local/global variable situation needs to be obfuscated more ( :P ). CAB, this situation would be kind of an answer to your question (but the "real life" answer would be more like: never. ...maybe an end user pick point list...?)
Code: [Select]
( (lambda ( / aif)

   (defun aif ( var expr iffalse )
    ;; anaphoric-if
    ;;
    ;; ported to AutoLisp from ?comonLisp?
    ;; I dont know the whole extents of the CL`aif' function, im only
    ;; working from one small definition.
    ;;
    ;; EX:
    ;;     (aif 'a (getint "\nEnter new value [1]: ") 1)
    ;;
    (set var (cond (expr) (iffalse))) )
   ;; Resirected for argument sake. ...*lmao*

   (initget 6)
   (aif 'rad (getdist "\nCircle Radius [1]: ") 1)

   (aif 'seg (getint "\nCircle Segmemts [16]: ") 16)

   (setq ang 0
    inc (/ (* pi 2) seg)
    p 0)

   (repeat seg
    (set (read (strcat "P" (itoa p)))
     (polar '(0 0 0) ang rad))

    (setq ang (+ ang inc)
     p (1+ p)))

   )
 )
 

( (lambda ( / let)

    (defun let (bindings body)
      ;; let for autolisp.
      ;;
      ;; Ex:
      ;; (let '((x 10) (y 20))                         ; bindings
      ;;      '((princ "\nLocal values of X and Y: ")  ; body
      ;;        (prin1 x)
      ;;        (prin1 y)
      ;;        (princ)
      ;;     )
      ;;   )
      ;;
      (eval
        (cons (append (list 'lambda (mapcar 'car bindings)) body)
              (mapcar 'cadr bindings))) )

    (initget 6)
    (let
      ;; bindings
      '(
        (rad
          (cond
            ((getdist "\nCircle Radius [1]: "))
            (1))
          )
        (seg
          (cond
            ((getint "\nCircle Segmemts [16]: "))
            (16))
          )
        )

      ;; body
      '(
        (setq ang 0
              inc (/ (* pi 2) seg)
              p 0)

        (repeat seg
                (set (read (strcat "P" (itoa p)))
                     (polar '(0 0 0) ang rad))

                (setq ang (+ ang inc)
                      p (1+ p))
                (print (read (strcat "P" (itoa p))))
                )

        )
      )
    )
 )


;; (mapcar
;;   '(lambda ( x ) (setq x nil))
;;   '(P1 P2 P3 P4 P5 P6 P7 P8 P9 P10 P11 P12 P13 P14 P15 P16)
;;   )

:D
Title: Re: SET & SETQ alla Kenny
Post by: Lee Mac on April 20, 2010, 03:29:04 PM
Nice one Se7en, I like that 'let' function  :lol:
Title: Re: SET & SETQ alla Kenny
Post by: JohnK on April 20, 2010, 04:57:45 PM
[ http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-12.html#%_sec_1.3.2 ]

Yeah its cool (academically speaking though) but if you use it in real life most people will just make fun of you. I just posted it to be fun. BTW, the same goes for that AIF function as well (use the method, not the function).

Snip from my `let' notes.
 
Code: [Select]

  ;; ...
  ;; Synopsis: let <bindings> <body>
  ;;
  ;;  (let '((a (+ 1 (* x y)))
  ;;         (b (- 1 y)))
  ;;     '(+ (* x (square a))
  ;;         (* y b)
  ;;         (* a b)))
  ;;  ~~>
  ;;  ((LAMBDA nil (+ (* X (SQUARE (+ 1 (* X Y))))
  ;;  (* Y (- 1 Y))
  ;;  (* (+ 1 (* X Y)) (- 1 Y)))))
  ;;
  ;;  ==> Evaluated value
  ;;
  ;; EX1:
  ;;  (setq x 5)
  ;;  (+ (let '((x 3))
  ;;          '(+ x (* x 10)))
  ;;   x)
  ;; 
  ;;  ==> 38
  ;; 
  ;; EX2:
  ;;  (setq x 2 y 4)
  ;;  (let '((x 3)
  ;;         (y (+ x 2)))
  ;;       '(* x y))
  ;; 
  ;;  ==> 12
  ;; ...
Title: Re: SET & SETQ alla Kenny
Post by: MP on April 20, 2010, 05:10:19 PM
*cough* (http://www.theswamp.org/index.php?topic=27226.msg328322#msg328322)
Title: Re: SET & SETQ alla Kenny
Post by: CAB on April 20, 2010, 05:49:34 PM
Oh another link I need to add to my Swamp Index  :-)
Title: Re: SET & SETQ alla Kenny
Post by: JohnK on April 20, 2010, 06:08:11 PM
Oh another link I need to add to my Swamp Index  :-)

Ive been trying to think of a good method for storing information other then my pea sized grey matter but i either get too busy to save a text file into my local directory or i just get too backed up with links.  ...we need to enable that SVN feature of theSwamp and start submitting these solutions or something because there is just way too much for one person to do.
Title: Re: SET & SETQ alla Kenny
Post by: Lee Mac on April 20, 2010, 06:12:12 PM
Thanks Se7en, I'm going to have to get stuck into that eBook at some point after my exams are over - I've browsed the first few sections, and its a great book.

Title: Re: SET & SETQ alla Kenny
Post by: Lee Mac on April 20, 2010, 06:21:02 PM
*cough* (http://www.theswamp.org/index.php?topic=27226.msg328322#msg328322)

Great explanation Michael - even if you hadn't had your coffee  :-)  I particularly like the part about indirect addressing - I hadn't considered using the function in that way, it reminds me of using pointers.

Thanks
Title: Re: SET & SETQ alla Kenny
Post by: MP on April 20, 2010, 10:07:04 PM
Great explanation Michael - even if you hadn't had your coffee  :-)  I particularly like the part about indirect addressing - I hadn't considered using the function in that way, it reminds me of using pointers. Thanks

Thank you Lee. I first encountered the term ''indirect addressing'' when programming HP hand helds in the mid 80s (FML). Then when programming PLCs. When I finally got around to learning C in the early 90s it wasn't hard at all to wrap my head around the concept, kinda like "but of course".
Title: Re: SET & SETQ alla Kenny
Post by: JohnK on April 21, 2010, 08:31:58 AM
In the mid 80s i was...
In the 90s i was...

/me runs away laughing so he doesn't get hit.
Title: Re: SET & SETQ alla Kenny
Post by: CAB on April 21, 2010, 08:43:24 AM
Yea you young Whipper Sappers just be aware that time marches on and faster than you think.
You too will look back and say what happened to the past 20 years. So my advice is to take a
little time & plan for it.  :evil:
Title: Re: SET & SETQ alla Kenny
Post by: JohnK on April 21, 2010, 08:51:10 AM
wut? How do i plan for that?! Get my cane and dippers now (stock up so-to-speak)?





:D
Title: Re: SET & SETQ alla Kenny
Post by: Chuck Gabriel on April 21, 2010, 09:01:50 AM
Be nice to your kids so they they don't abuse you when you are to frail to defend yourself.  Things like that ...
Title: Re: SET & SETQ alla Kenny
Post by: JohnK on April 21, 2010, 09:03:55 AM
lamo. Yeah i can see that. Noted.
Title: Re: SET & SETQ alla Kenny
Post by: Lee Mac on April 21, 2010, 10:13:15 AM
 :evil: