Author Topic: SET & SETQ alla Kenny  (Read 6127 times)

0 Members and 1 Guest are viewing this topic.

curmudgeon

  • Newt
  • Posts: 192
SET & SETQ alla Kenny
« 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:
Never express yourself more clearly than you are able to think.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10362
Re: SET & SETQ alla Kenny
« Reply #1 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-)
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.

David Bethel

  • Swamp Rat
  • Posts: 639
Re: SET & SETQ alla Kenny
« Reply #2 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

« Last Edit: April 20, 2010, 10:27:32 AM by David Bethel »
R12 Dos - A2K

Lee Mac

  • Seagull
  • Posts: 12254
  • London, England
Re: SET & SETQ alla Kenny
« Reply #3 on: April 20, 2010, 08:19:30 AM »
Great explanation David  :-)

CAB

  • Global Moderator
  • Seagull
  • Posts: 10362
Re: SET & SETQ alla Kenny
« Reply #4 on: April 20, 2010, 09:25:50 AM »
Much better example David 8-)
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.

David Bethel

  • Swamp Rat
  • Posts: 639
Re: SET & SETQ alla Kenny
« Reply #5 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
R12 Dos - A2K

CAB

  • Global Moderator
  • Seagull
  • Posts: 10362
Re: SET & SETQ alla Kenny
« Reply #6 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.
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.

Lee Mac

  • Seagull
  • Posts: 12254
  • London, England
Re: SET & SETQ alla Kenny
« Reply #7 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.


David Bethel

  • Swamp Rat
  • Posts: 639
Re: SET & SETQ alla Kenny
« Reply #8 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
R12 Dos - A2K

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9243
Re: SET & SETQ alla Kenny
« Reply #9 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
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--

Lee Mac

  • Seagull
  • Posts: 12254
  • London, England
Re: SET & SETQ alla Kenny
« Reply #10 on: April 20, 2010, 03:29:04 PM »
Nice one Se7en, I like that 'let' function  :lol:

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9243
Re: SET & SETQ alla Kenny
« Reply #11 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
  ;; ...
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--

MP

  • Seagull
  • Posts: 17399
Re: SET & SETQ alla Kenny
« Reply #12 on: April 20, 2010, 05:10:19 PM »
\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

CAB

  • Global Moderator
  • Seagull
  • Posts: 10362
Re: SET & SETQ alla Kenny
« Reply #13 on: April 20, 2010, 05:49:34 PM »
Oh another link I need to add to my Swamp Index  :-)
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.

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 9243
Re: SET & SETQ alla Kenny
« Reply #14 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.
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--