From: Trastabuga on 30 Apr 2010 12:08 Hi I have a simple task: Given a class, I need a function that can add elements to one of the fields of the class. It's easy to do if the function knows the name of the field, but what if the function doesn't know it and we need to pass it as an input parameter to the function. Let's say (defclass test1 () ((fld1 :accessor test1-fld1 :initarg :fld1 :type list))) (setq o (make-instance 'test1 :fld1 nil)) (defun append-element (o el) (with-slots (fld1) o (setf fld1 (append fld1 (list el))))) How to modify the append-element function so that it doesn't know about the fld1 and had semantics of (append-element o el field-name) so we can call it like (append-element o 'a 'fld1)?
From: Trastabuga on 30 Apr 2010 12:36 On Apr 30, 12:08 pm, Trastabuga <lisper...(a)gmail.com> wrote: > Hi > > I have a simple task: > Given a class, I need a function that can add elements to one of the > fields of the class. > It's easy to do if the function knows the name of the field, but what > if the function doesn't know it and we need to pass it as an input > parameter to the function. > Let's say > (defclass test1 () > ((fld1 :accessor test1-fld1 :initarg :fld1 :type list))) > > (setq o (make-instance 'test1 :fld1 nil)) > (defun append-element (o el) > (with-slots (fld1) o > (setf fld1 (append fld1 (list el))))) > > How to modify the append-element function so that it doesn't know > about the fld1 and had semantics of (append-element o el field-name) > so we can call it like (append-element o 'a 'fld1)? I am sure, it can be done as a macro (defmacro append-element (o el field) `(with-slots (,field) o (setf ,field (append ,field (list ,el))))) But I'd like to know if it's possible to pass some kind of setf-able place to the function?
From: Frode V. Fjeld on 30 Apr 2010 12:39 Trastabuga <lispercat(a)gmail.com> writes: > (defclass test1 () > ((fld1 :accessor test1-fld1 :initarg :fld1 :type list))) > > (setq o (make-instance 'test1 :fld1 nil)) > (defun append-element (o el) > (with-slots (fld1) o > (setf fld1 (append fld1 (list el))))) > > How to modify the append-element function so that it doesn't know > about the fld1 and had semantics of (append-element o el field-name) > so we can call it like (append-element o 'a 'fld1)? Not entirely sure I understand, but maybe the SLOT-VALUE accessor is what you're looking for? (defun append-element (object element slot) (setf (slot-value object slot) (append (slot-value object slot) (list element)))) -- Frode V. Fjeld
From: Peter Keller on 30 Apr 2010 12:45 Trastabuga <lispercat(a)gmail.com> wrote: > On Apr 30, 12:08?pm, Trastabuga <lisper...(a)gmail.com> wrote: > > I am sure, it can be done as a macro > (defmacro append-element (o el field) > `(with-slots (,field) o > (setf ,field (append ,field (list ,el))))) Whenever I see this form in a macro: (setf ,x (stuff ,x)) I think it is wrong due to multiple evaluation (from "On Lisp"). However, lots of people keep reiterating it in posts here. Are there well-known situations where it isn't wrong? -pete
From: Trastabuga on 30 Apr 2010 12:58 On Apr 30, 12:39 pm, "Frode V. Fjeld" <fr...(a)netfonds.no> wrote: > Trastabuga <lisper...(a)gmail.com> writes: > > (defclass test1 () > > ((fld1 :accessor test1-fld1 :initarg :fld1 :type list))) > > > (setq o (make-instance 'test1 :fld1 nil)) > > (defun append-element (o el) > > (with-slots (fld1) o > > (setf fld1 (append fld1 (list el))))) > > > How to modify the append-element function so that it doesn't know > > about the fld1 and had semantics of (append-element o el field-name) > > so we can call it like (append-element o 'a 'fld1)? > > Not entirely sure I understand, but maybe the SLOT-VALUE accessor is > what you're looking for? > > (defun append-element (object element slot) > (setf (slot-value object slot) > (append (slot-value object slot) > (list element)))) > > -- > Frode V. Fjeld Yes, that will do! Thank you Frode!
|
Next
|
Last
Pages: 1 2 3 4 Prev: Lispers in the silicon valley? Next: Online survey jobs & data entry jobs |