From: Thomas A. Russ on 21 Dec 2009 15:18 Pascal Costanza <pc(a)p-cos.net> writes: > On 21/12/2009 13:56, Cecil Westerhof wrote: > > I made a function to set a property in a list. My definition is that > > when the property not already exists that it is an error. > > Why do you want it to behave like that? What problem does this solve? > What do you want to do with this? I think Pascal's question was about why you want setting a non-existing property to be an error rather than just setting it? After all, usually when you want a property set, you really care about it being set AFTERWARDs, and don't really care if it had a value beforehand. -- Thomas A. Russ, USC/Information Sciences Institute
From: Thomas A. Russ on 21 Dec 2009 15:26 Cecil Westerhof <Cecil(a)decebal.nl> writes: > I made a function to set a property in a list. My definition is that > when the property not already exists that it is an error. (I have to > make also a function create-property-in-list.) There needs to be more > error checking: are property-1 and property-2 really keywords, and it > has to be made more general (now it works only for dept 2, it should > work for every -reasonable- dept). But before I continue I would like to > know if I am on the right track, or should better it implement > differently? > > The function: > (defun set-property-in-list(this-list value property-1 property-2) > (let ((sub-list) > (property-value)) > (setq sub-list (getf this-list property-1) > property-value (getf sub-list property-2)) > (if (not property-value) > (format nil "Tried to change a non-existing property") > (progn (setf (getf sub-list property-2) value) > nil)))) Well, for starters, you could make this a bit simpler by using LET* and eliminating the SETQs. Then you could also use the exception mechanism if you really wanted to have an error signaled. And that would also have the benefit that you don't have to adopt a string vs NIL return value to tell if you have an error condition. So, here is a more idiomatic lisp solution that does roughly what your function does: (defun set-sub-property (property-list value property sub-property) (let* ((sub-list (getf property-list property)) (current-value (getf sub-list sub-property))) (if current-value (setf (getf sub-list sub-property) value) (error "Tried to set non-existent property (~S ~S) on ~S" property subproperty property-list)))) But a more useful solution would be to add the property value to the list. Note that implementing that really requires that you make sure you use the return value from setting the property -- you won't be able to rely on destructive mutation of your inputs. You would then put that code in place of the error call. -- Thomas A. Russ, USC/Information Sciences Institute
From: Pascal J. Bourguignon on 21 Dec 2009 19:02 Cecil Westerhof <Cecil(a)decebal.nl> writes: > Pascal Costanza <pc(a)p-cos.net> writes: > >> On 21/12/2009 13:56, Cecil Westerhof wrote: >>> I made a function to set a property in a list. My definition is that >>> when the property not already exists that it is an error. >> >> Why do you want it to behave like that? What problem does this solve? >> What do you want to do with this? > > Twofold. I want to learn Lisp, and I want some useful functionality. > > I am working with Emacs (that is how I came to lisp) and in Emacs I also > use GNUS. I made a daemon function to expire articles and fetch articles > in idle time. For this I have defined: > (defvar gnus-wait-times > (list :expire (list :idle 3 > :wait (- (/ +seconds-in-day+ +seconds-in-minute+) 2)) > :get-news (list :idle 2 > :wait 29)) > "Values for check in the idle daemon") > > I want to expire articles once a day and only when Emacs has been idle > for at least 3 minutes. Getting news (newsgroup messages and e-mail) I > want every 30 minutes and only when Emacs has been idle for at least two > minutes. (Getting news takes a lot less time as expiring articles. I use > 29 minutes, because fetching articles takes some time, but less as a > minute.) What about: (defstruct activity task period idle) (defvar *activity-queue* '()) (defun activity-enqueue (activity) (push acttivity *activity-queue*)) .... (defun expire-gnus-articles () ...) (defun get-new-gnus-articles () ...) (activity-enqueue (make-activity :task (function expire-gnus-articles) :idle 3 :wait (- (/ +seconds-in-day+ +seconds-in-minute+) 2))) (activity-enqueue (make-activity :task (function get-new-gnus-articles) :idle 2 :wait 29)) -- __Pascal Bourguignon__ http://www.informatimago.com/ CONSUMER NOTICE: Because of the "uncertainty principle," it is impossible for the consumer to simultaneously know both the precise location and velocity of this product.
From: Cecil Westerhof on 21 Dec 2009 20:08 pjb(a)informatimago.com (Pascal J. Bourguignon) writes: > What about: > > > (defstruct activity > task > period > idle) > > (defvar *activity-queue* '()) > (defun activity-enqueue (activity) (push acttivity *activity-queue*)) > ... > > > (defun expire-gnus-articles () ...) > (defun get-new-gnus-articles () ...) > > (activity-enqueue (make-activity :task (function expire-gnus-articles) > :idle 3 > :wait (- (/ +seconds-in-day+ +seconds-in-minute+) 2))) > > (activity-enqueue (make-activity :task (function get-new-gnus-articles) > :idle 2 > :wait 29)) New things to digest. Maybe I understand it wrong, but this looks like a onetime definition. What I want is to change the idle and wait values. By the way, it already works. The important parts of the code are reachable through: http://www.decebal.nl/EmacsLisp/sources/gnus-idle-daemon -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof
From: Pascal J. Bourguignon on 21 Dec 2009 21:04 Cecil Westerhof <Cecil(a)decebal.nl> writes: > pjb(a)informatimago.com (Pascal J. Bourguignon) writes: > >> What about: >> >> >> (defstruct activity >> name >> task >> period >> idle) >> >> (defvar *activity-queue* '()) >> (defun activity-enqueue (activity) (push acttivity *activity-queue*)) >> ... >> >> >> (defun expire-gnus-articles () ...) >> (defun get-new-gnus-articles () ...) >> >> (activity-enqueue (make-activity :name 'expire >> :task (function expire-gnus-articles) >> :idle 3 >> :wait (- (/ +seconds-in-day+ +seconds-in-minute+) 2))) >> >> (activity-enqueue (make-activity :task (function get-new-gnus-articles) >> :name 'get-news >> :idle 2 >> :wait 29)) > > New things to digest. Maybe I understand it wrong, but this looks like a > onetime definition. What I want is to change the idle and wait values. You can easily change a field of a structure: (setf (activity-idle (find-activity 'expire)) 5) > By the way, it already works. The important parts of the code are > reachable through: > http://www.decebal.nl/EmacsLisp/sources/gnus-idle-daemon Apart from reading PCL, I'd advise you to read SICP. SICP = Structure and Interpretation of Computer Programs http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-4.html http://swiss.csail.mit.edu/classes/6.001/abelson-sussman-lectures/ http://eli.thegreenplace.net/category/programming/lisp/sicp/ http://www.codepoetics.com/wiki/index.php?title=Topics:SICP_in_other_languages -- __Pascal Bourguignon__ http://www.informatimago.com/ READ THIS BEFORE OPENING PACKAGE: According to certain suggested versions of the Grand Unified Theory, the primary particles constituting this product may decay to nothingness within the next four hundred million years.
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: Mathematica emulation Next: I have almost given up on lisp |