Common Lisp: non-nil arguments and their names to alist, how? -
i quite new common lisp , programming, , i'm trying write function turns non-nil args alist. way can think of far is:
(let ((temp nil)) (if arg1 (setf temp (acons 'arg1 arg1 nil))) (if arg2 (setf temp (acons 'arg2 arg2 temp))) ... (if arg20-ish (setf temp (acons 'arg20-ish arg20-ish temp))) (do-something-with temp)) which not seem elegant, messy many arguments , when these need changed. i looking smarter way this, both sake of writing particular function , learning how think in lisp and/or functional programming.
the tricky part me figuring out how names of arguments or symbol use, without hand coding each case. if &rest provided arg names easy filter out nils loop or mapcar, since doesn't, can't see how "automate" this.
i'm totally interested in other solutions 1 described, if people think way unnatural.
edit: below example of trying do:
an object created, non-fixed number of data pairs , tags, e.g.:
user = "someone" creation-time = (get-universal-time) color-of-sky = "blue" temperature-in-celsius = 32 language = "common lisp" ... tags = '("one" "two" "three") these properties (i.e. key/arg names) different each time. new object added collection; thought array might work since want constant access time , need numeric id.
collection hold more , more such custom objects, indefinitely.
want able access objects matching combination of of tags used in these objects.
since array supposed store more , more data on long period, don't want parse every item in each time need search tag. store index of each object given tag in hash-table, under tag name. have written function, find difficult figuring out how collect data , turn alist or can parse, index, , store.
this macro define function turns non-nil arguments alist bound during execution of body:
(defmacro defnamed (fun-name alist-sym (&rest args) &body body) `(defun ,fun-name (,@args) (let ((,alist-sym)) ,@(mapcar (lambda (s) `(when ,s (push (cons ',s ,s) ,alist-sym))) (reverse args)) ,@body))) demonstration:
(defnamed make-my alist (a b c) alist) (make-my 1 nil 3) => ((a . 1) (c . 3))
Comments
Post a Comment