Author Topic: The power of the list  (Read 5465 times)

0 Members and 1 Guest are viewing this topic.

Mark

  • Custom Title
  • Seagull
  • Posts: 28753
The power of the list
« on: October 29, 2003, 02:17:14 PM »
As we all know the 'LISP' in AutoLISP stands for LISt Processing. I see many people writing code that do not use the power of the list. The lisp language is made for working with lists, and includes many powerful tools to do so. One of my favorites is MAPCAR. Below is a little sample of it's power, it's not much but I think you'll get the idea. Hopefully somebody else will come along and supply us with some more examples of working with lists.


Code: [Select]
(defun c:mapcar-test (/ divide_by_2 l1 l2)
  (defun divide_by_2 (n)
     (/ n 2)
     )

(repeat 4
  (initget 7)
  (setq l1 (cons (getint "\nEnter an Integer: ") l1))
  )
   
 (setq l2 (reverse (mapcar 'divide_by_2 l1)))

  )


 
 comments, corrections and additions welcome
TheSwamp.org  (serving the CAD community since 2003)

Cider

  • Newt
  • Posts: 60
The power of the list
« Reply #1 on: February 20, 2004, 06:08:54 AM »
few days ago i tried to do a workaraound (it´s no more necessary thanks to stig madsen)
and worked out a piece of code like this:
Code: [Select]
(Setq p_data
"(setq pos_data (vlax-make-safearray vlax-vbstring '(0 . 5) '(0 . zappa)))")
(eval (read p_data)  


it looked funny, but it worked.(what i wanted to do was changing the value of
"zappa" at runtime, i used (vl-string-subst) to substitue "zappa" with different values)
         
i dwelled on this topic a little more and tried somehting like this:
Code: [Select]
(defun s ()
      (setq r '(setq pos_data (vlax-make-safearray vlax-vbstring '(0 . 5) '(0 . 8))))
      (eval r))
     


now this looked a bit more professional. when i wondered about the possibilities i was
stunned. i went through some documentations because i wanted to create functions out of
 a list. i came to this:
Code: [Select]
     (defun x ()
 
  (Setq a '((o p) (* o 3 p)))
  (defun-q-list-set 'n a)
  )
     


this concept is completely new to me. with a few list-editing commands one can modify
or even create expressions/functions while the programm is executed.wow!
         
now this looks very promising but is there a practical usage of this feature? or is
this mainly used for some rare and exotic occasions?

S! Cider
due to budget cuts, the lights at the end od the tunnel willbe turne d off.   

Win10 , Bricscad 10-18

SMadsen

  • Guest
The power of the list
« Reply #2 on: February 20, 2004, 09:04:53 AM »
That is really cool, Cider. You're into something that normally takes years to grasp or even care about: evaluation of lisp expressions.
My compliments.

There are some practical usages for that kind of processing. The most common application is appending to the startup function - as you can see examples of in the help for DEFUN-Q-LIST-SET.
But also if you wrote, say, a graphical calculator and wanted to create functions on the fly from user inputs, you would need to handle functions as lists and evaluate them on demand.

Besides the practical usages, playing around with quoted lists and evaluators such as EVAL (naturally) and LAMBDA is a doorway to greater understanding of how lisp works.

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
The power of the list
« Reply #3 on: February 20, 2004, 12:57:45 PM »
whaaaaa? I got lost are you trying to make a "higher-order-procedure"?
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
The power of the list
« Reply #4 on: February 20, 2004, 03:52:51 PM »
Im serious, Im really not trying to be a smartass or anything. Ive been trying to wrap my head arround this subject and this is the results of my thinkings. am I even close?

Quote
Command: (set 'a '(+ (+ 1 2) 4))
(+ (+ 1 2) 4)

Command: !a
(+ (+ 1 2) 4)

Command: (set 'b a)
(+ (+ 1 2) 4)

Command: (eval a)
7

Command: (eval b)
7

Command: (defun id (x) x)
ID

Command: (id a)
(+ (+ 1 2) 4)

Command: (id b)
(+ (+ 1 2) 4)

Command: !a
(+ (+ 1 2) 4)

Command: !b
(+ (+ 1 2) 4)
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
The power of the list
« Reply #5 on: February 20, 2004, 04:13:22 PM »
Would this be an example as to what your looking to do? --kinda sorta?! (I mean this is "dummed down"  bit cause I dont have alot of time to build a huge example ...but I hope you get what I mean.)

Basicly what this is ment to represent is to do a specific operation based on "outside" or "always changing" results.

Code: [Select]
(defun You_do_the_math (a b)
  (defun aProcedure (process a b) (process a b))
  ;; define a procedure to accept a process
  (initget 0 "+ - / *")
  (setq Inp (getkword "\nEnter a process to preform [+, -, /, *] </>:"))
  ;; get the math operator
  (aProcedure
    (cond ((= inp "+") +)
          ((= inp "-") -)
          ((= inp "*") *)
          (/))
    a b)
  ;; do the math
  )
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
The power of the list
« Reply #6 on: February 20, 2004, 11:54:25 PM »
I am still scratching my head on this one. Are we talking about this type of scenario?

Code: [Select]

(defun-q C:MYCOMMAND()
 (dothis here)
 (setq C:MYCOMMAND (subst '(dothis there) '(dothis here) C:MYCOMMAND))


Obviously this code will not work, but I still need a bit more info on what we are attempting to accomplish here. Is it merely dynamic functions we are looking at?
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
The power of the list
« Reply #7 on: February 21, 2004, 01:16:17 AM »
This is a reasonable example of lists/functions.

I use this sort of thing for creating lsp routines to load & run VBA Macros

Code: [Select]

;;;Bare bones.
(defun c:loadalias-vbamacro (/ macrolist vbafile)
  ;;                        blnUnload  strAlias  strFileName  strMacroName                  
  (setq macrolist (list (list 1   "alias-xxx" "drawline.dvb" "drawline")
                        (list nil "alias11"   "file12.dvb"   "macro12" )
                  )
  )
  (foreach entry macrolist
    (if (setq vbafile (findfile (caddr entry)))
      (eval (list 'defun
                  (read (strcat "C:" (cadr entry)))
                  '( / tmp)
                  (list 'vla-runmacro
                        '(setq tmp (vlax-get-acad-object))
                        (strcat vbafile "!" (cadddr entry))
                  )
                  (if (car entry)
                    (list 'vla-unloaddvb 'tmp vbafile)
                    '(princ)
                  )
                  '(vlax-release-object tmp)
                  '(princ)
            )
      )
    )
  )
  (princ)
)
(c:loadalias-vbamacro)
(princ)  



Code: [Select]

;; Sample Output  

;; leave *.dvbloaded
;;(list nil "alias-xxx" "drawline.dvb" "drawline")
(defun c:alias-xxx (/)
  (vla-runmacro (setq tmp (vlax-get-acad-object))
                "P:\\_visualLispStuff\\Proggys\\drawline.dvb!drawline"
  )
  (princ)
  (vlax-release-object tmp)
  (princ)
)
;;
;; Unload *.dvb
;;(list 1 "alias-xxx" "drawline.dvb" "drawline")
(defun c:alias-xxx (/ tmp)
  (vla-runmacro (setq tmp (vlax-get-acad-object))
                "P:\\_visualLispStuff\\Proggys\\drawline.dvb!drawline"
  )
  (vla-unloaddvb tmp "P:\\_visualLispStuff\\Proggys\\drawline.dvb")
  (vlax-release-object tmp)
  (princ)
)




Or this

Code: [Select]

;|
FileName :  VBA-DefMacro.lsp
Function  :
VBA:DefMacro
 
strLspName   "c:test"
strFileName   "s:/Proggys/Drawline.dvb"
strMacroName  "Module1.DrawLine"
blnUnload     nil  ;; use 1 if Unload is Required
 

Load this file via Startup Suite into each Document Space

Kerry B 2004Jan10
Issue For Beta Testing.
|;
(vl-load-com)

(defun VBA:DEFMacro (strLspName strFileName strMacroName blnUnload /)
  (or gobj-AcadApplication
      (setq gobj-AcadApplication (vlax-get-acad-object))
  )
  (if (setq strFileName (findfile strFileName))
    (eval (list 'defun
                (read strLspName )
                '(/)
                (list 'vla-runmacro
                      gobj-AcadApplication
                      (strcat strFileName "!" strMacroName)
                )
                (if blnUnload
                  (list 'vla-unloaddvb gobj-AcadApplication strFileName)
                  '(princ)
                )
                '(princ)
          )
    )
  )
  (princ)
)
(princ)
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.

JohnK

  • Administrator
  • Seagull
  • Posts: 10605
The power of the list
« Reply #8 on: February 21, 2004, 08:32:53 AM »
OOOOOoooh I get cha! All your basicly doing is storing a set of procedures and creating the right one based on stuff the fly. --Right?--

This is what i was kinda thinking about when i did posted the "sans variables" post. You could create a list of procedures and execute the ones based on the needs of the user. (Or if the user enviroment "passed a test" or something.) That way the full blown app dosent have to be loaded. Some of it could be loaded into the stack instead, and retrieved at a moments notice. --Right?--

:P
TheSwamp.org (serving the CAD community since 2003)
Member location map - Add yourself

Donate to TheSwamp.org

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
The power of the list
« Reply #9 on: February 21, 2004, 02:38:43 PM »
Ok,  now I understand ....

Basically it is autoload on steroids

For example in the Acad200xdoc.lsp
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

Cider

  • Newt
  • Posts: 60
The power of the list
« Reply #10 on: February 23, 2004, 08:38:49 AM »
oops, didn´t want to create confusion :) .
i´ve been intrigued by the possibilities calling expressions/functions from a list. as posted, this concept is completely new to me and i wondered about the usage of this feat.

thx for the input!

S! Cider
due to budget cuts, the lights at the end od the tunnel willbe turne d off.   

Win10 , Bricscad 10-18