Author Topic: Lisp Objects  (Read 2672 times)

0 Members and 1 Guest are viewing this topic.

Jeff H

  • Needs a day job
  • Posts: 6150
Lisp Objects
« on: March 05, 2011, 01:12:25 AM »
Out of curiosity,

No code needed unless you feel like posting code,
but was wondering if someone could explain briefly how you would go about doing this.


How would you print 10 people's First Name, Last Name, and age.

By print I meant to console or acad, but does not matter more interested in how you do it.

Do you create 10 people objects?
Or 3 separate list containing the info for each person at the same nth in each list?

As mentioned earlier not asking for someone to code for me just wondering if it was not to trouble to explain in a sentence or two.

Thanks

gile

  • Gator
  • Posts: 2520
  • Marseille, France
Re: Lisp Objects
« Reply #1 on: March 05, 2011, 04:02:33 AM »
Hi,

AutoLISP do not know 'objects'. It only knows few types:

integer: INT (int32)
double: REAL
string: STR
symbol: SYM

and somme others related to AuoCAD:

entity name: ENAME (close to ObjectId)
selection set: PICKSET
functions: SUBR, USUBR, EXRXSUBR
ActiveX object: VLA-OBJECT
variant: VARIANT
safearray: SAFEARRAY

An, of course, list: LIST.

IMO the closest AutoLISP 'thing' to an object with properties should be an association list (which are closed to .NET Dictionaries). It's the way used by AutoLISP to expose entities DXF properties.

So an AutoLISP 'Person' could a list like:
Code: [Select]
((0 . lastName) (1 . firstName) (70 . age))or more explicitly:
Code: [Select]
(("LastName" . lastName) ("FirstName" . firstName) ("Age" . age))
Speaking English as a French Frog

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Lisp Objects
« Reply #2 on: March 05, 2011, 04:10:23 AM »
I would use the second example from gile ..

< .. > or more explicitly:
Code: [Select]
(("LastName" . lastName) ("FirstName" . firstName) ("Age" . age))

persons can be collected into a list of people easily

a collection can be sorted or searched relatively easily

not at efficient or self contained as objects, but 'functional' to suit the language.
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.

Jeff H

  • Needs a day job
  • Posts: 6150
Re: Lisp Objects
« Reply #3 on: March 05, 2011, 04:22:05 AM »
Thanks guys

So something like

Code: [Select]
((("1LastName" . lastName) ("1FirstName" . firstName) ("1Age" . age)) (("2LastName" . lastName) ("2FirstName" . firstName) ("2Age" . age)))......more parenthess

dgorsman

  • Water Moccasin
  • Posts: 2437
Re: Lisp Objects
« Reply #4 on: March 05, 2011, 04:56:52 AM »
For only ten names, dotted pair list indexed by a unique "username" e.g.

Code: [Select]
(("username1" ("Lastname" . "Some last name") ("Firstname" . "Some other first name"))("username2" ("Lastname" . "A last name")))
If you are going to fly by the seat of your pants, expect friction burns.

try {GreatPower;}
   catch (notResponsible)
      {NextTime(PlanAhead);}
   finally
      {MasterBasics;}

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Lisp Objects
« Reply #5 on: March 05, 2011, 05:12:51 AM »
I'd probably start something like this :

Code: [Select]
(setq var:people '())


(defun make_person (LastName FirstName Age / index perp perpDef )
  (setq index      (1+ (length var:people))
        perp       (list (cons "LastName" LastName) (cons "FirstName" FirstName) (cons "Age" Age))
        perpDef    (cons (cons "ID" index) list perp)
        var:people (cons perpDef var:people)
  )
)


(defun define_person ( / LastName FirstName Age)
  (if
    (and (and (setq LastName (getstring nil "\nLastName ")) (/= (strlen LastName) 0))
         (and (setq FirstName (getstring nil "\nFirstName ")) (/= (strlen FirstName) 0))
         (or (initget 7) (setq Age (getint "\nAge ")))
    )
     (make_person LastName FirstName Age)
  )
)


(defun count_population ()
  (alert (strcat "Population is " (itoa (length var:people))))
)

(defun initial_populate ()
  (make_person "Bloggs" "Joe" 42)
  (make_person "Duck" "Fred" 5)
  (make_person "Mouse" "Minnie" 69)
)

;;----------------------------
(defun c:doit ()
  (initial_populate)
  (count_population)
  (while (define_person) (count_population))
  (princ)
)
(princ)


;;----------------------------



« Last Edit: March 05, 2011, 05:25:40 AM by Kerry »
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: Lisp Objects
« Reply #6 on: March 05, 2011, 05:34:41 AM »
Then something like :

Code: [Select]


(defun peep_census ()
  (foreach peep var:people    
    (princ (cdr (assoc "ID" peep)))
    (terpri)
    (princ (strcat (cdr (assoc "LastName" peep))
                   ", "
                   (cdr (assoc "FirstName" peep))
                   ": "
                   (itoa (cdr (assoc "Age" peep)))
           )
    )
    (terpri)
  )
  (princ)
)


Quote


Command: (peep_census)
4
Roark, Howard: 68
3
Mouse, Minnie: 69
2
Duck, Fred: 5
1
Bloggs, Joe: 42


Of course, these are all very primitive and far from bomb-proof but could be a place to start.
« Last Edit: March 05, 2011, 05:40:57 AM by Kerry »
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.

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Lisp Objects
« Reply #7 on: March 05, 2011, 10:56:01 PM »
I tend to use fixed list structures
Code: [Select]
(defun c:test()
;;  Fixed List structure
;; ("LastName"  ; string
;;  "FirstName" ; string
;;   Age        ; intiger
;;  )
(setq data
       (list
         '("Bloggs" "Joe" 42)
         '("Duck" "Fred" 5)
         '("Mouse" "Minnie" 69)

       )
)
;;  print to command line, you know th position of each item
(foreach person data
  (princ "\nLast name: ")
  (princ (car person))
  (princ "\nFirst name: ")
  (princ (cadr person))
  (princ "\nLast name: ")
  (princ (itoa (caddr person)))
)

;;  If you need to impliment an index
(setq PersonIndex '("LastName" "FirstName" "Age"))

;; find first names
(setq ptr (vl-position "FirstName" PersonIndex))

;;  find Fred
(setq idx -1
      item "Fred"
      )
(vl-some
  (function
    (lambda (person)
       (and (= item (nth ptr person))
            (setq result person)))) data)
  (vl-princ-to-string result)
  (princ)
)
« Last Edit: March 05, 2011, 11:39:47 PM by CAB »
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.

hermanm

  • Guest
Re: Lisp Objects
« Reply #8 on: March 05, 2011, 11:06:58 PM »
As Gile said...

AutoLISP has no built-in objects, so if you want them, you'll have to create them.

For example:

http://www.theswamp.org/index.php?topic=29356.0

Code: [Select]
Command: (axstruct 'person (list 'FirstName 'LastName 'Age))
SET-PERSON-AGE

Command: (axstruct-get-type 'person)
(PERSON (FIRSTNAME . 0) (LASTNAME . 1) (AGE . 2))

Command: (setq JoeBlack (axstruct-new 'person '("Joe" "Black" 1000000)))
(PERSON #<safearray...>)

Command: (axstruct-getval joeblack 'firstname)
"Joe"

Command: (axstruct-getval joeblack 'age)
1000000

Command: (get-person-age joeblack)
1000000


« Last Edit: March 05, 2011, 11:28:18 PM by hermanm »

hermanm

  • Guest
Re: Lisp Objects
« Reply #9 on: March 06, 2011, 01:33:54 AM »
Here is a slight update:

Code: [Select]
Command: (setq Amelia (person-new '("Amelia" "Earhart" 29)))
(PERSON #<safearray...>)

Command: (person amelia)
(PERSON "Amelia" "Earhart" 29)

Command: (person-list amelia)
(PERSON "Amelia" "Earhart" 29)

Command: (person-age amelia)
29

Command: (person-get-age amelia)
29