Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!uakari.primate.wisc.edu!caen!uwm.edu!linac!att!att!cbnewsl!davel From: davel@cbnewsl.att.com (David Loewenstern) Newsgroups: comp.lang.lisp Subject: Re: Accessing a field of a Lisp structure Message-ID: <1991Apr11.215248.9075@cbnewsl.att.com> Date: 11 Apr 91 21:52:48 GMT References: <1991Apr10.215214.2827@cbnewsl.att.com> <28047A1F.44CE@ibma0.cs.uiuc.edu> Distribution: usa Organization: AT&T Bell Labs, Whippany, NJ Lines: 93 In article <28047A1F.44CE@ibma0.cs.uiuc.edu> epstein@sunc4.cs.uiuc.edu (Milt Epstein) writes: >In <1991Apr10.215214.2827@cbnewsl.att.com> davel@cbnewsl.att.com (David Loewenstern) writes: > >>In article mzaik@ee.eng.ohio-state.edu (Tahsin Mzaik) writes: >>> >>>I would like to write a macro that can access a particular field >>>in a structure. >>> >>>Example: Let the structure be: >>> (defstruct test a b c) >>> >>> (make-test instance) >>> >>>I would like to wite a macro 'access-field' that I can invoke >>>something like >>> >>> (access-field instance 'a) >> >>Try: >>(defmacro access-field (instance field) >> `(,(intern (format nil "~A-~A" 'test field)) ,instance)) >> >>Note that this will allow >>(access-field instance a) >>to work but not, for example >>(access-field instance (get-slot-name-from-some-wierd-place)) > >Someone else also posted this question just a couple of days ago, to >which Barry Margolin responded suggesting a function like: > >(defun access-field (instance field) > (case field > (a (test-a instance)) > (b (test-b instance)) > (c (test-c instance)))) > >Note that if you use a function, it doesn't matter where you get the >slot-name from. Oh, I agree. It is, however, slower. > >I sent a message to Barry suggesting the INTERN-FORMAT version, and he >pointed out a couple of things: > >1. It's better to use FIND-SYMBOL than INTERN, since the symbol (the >structure accessor function) should already exist. Maybe. The cost of using find-symbol is that the macro writer must add some error handling code, or else (access-field instance oops) will generate some unreadable nonsense about test-NIL being undefined. Besides, while it is good coding style, and helps the compiler generate better code, to define before using a structure, it is legal to compile (access-field instance a) or even (test-a instance) before loading the definition of test. > >2. The CASE version appeared to be much faster (he did some timing >test on a Symbolics). This, however, I do not believe. After all, the macro (access-field instance a) expands during compile time directly into (test-a instance) The function access-field *calls* test-a and so cannot possibly be faster than test-a alone. Worse, the macro (access-field instance c) expands into (test-c instance), and during run-time is no slower than (access-field instance a). The function access-field is O(n), where n is the number of slots in the test structure. The only way the function could be faster than the macro is if you are including the macroexpansion time with the execution time. Comments, Barry? > >-- >Milt Epstein >Department of Computer Science >University of Illinois >epstein@cs.uiuc.edu David Loewenstern AT&T Bell Laboratories 14B-253 Whippany, NJ 07981 email: davel@whutt.att.com || whutt!davel at&t: 201-386-6516