[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to describe something in Lisp?
From: |
Ted Zlatanov |
Subject: |
Re: How to describe something in Lisp? |
Date: |
Fri, 06 Feb 2009 12:53:47 -0600 |
User-agent: |
Gnus/5.110011 (No Gnus v0.11) Emacs/23.0.60 (gnu/linux) |
On Thu, 5 Feb 2009 08:22:27 +0100 Johan Andersson <johan.rejeep@gmail.com>
wrote:
JA> For the mode I was asking about, regular p-lists are fine. But I'm planning
JA> to do another mode. In this mode I will have a general class and one
JA> subclass for each programming language, where each subclass defines what
JA> should be done in that particular language. After your description I think
JA> defclass will work best since there's inheritance involved.
You might like this, which I think is really nice because it's
self-describing and very clean. I didn't write it :)
Ted
;;; from J.V. Toups
(defun toups-bang (sym)
(intern (format "%s!" sym)))
(defun toups-s-cat (sym1 sym2)
(intern (format "%s-%s" sym1 sym2)))
(defun toups-ques (sym)
(intern (format "%s?" sym)))
(defmacro defstruquine (name &rest slots)
(let* ((n-fields (length slots))
(i 1)
(out `(progn
(defun ,(toups-bang name) ,slots
(list ',(toups-bang name) ,@slots))
(defun ,(toups-ques name) (item)
(eq (car item) ',(toups-bang name))))))
(loop for slot in slots do
(setf out
(append out
(list `(defun ,(toups-s-cat name slot) (item) (elt item ,i)))))
(setf i (+ i 1)))
(append out (list nil))))
;; Which can be used thusly:
;; (defstruquine person first-name last-name age)
;; (let ((p (person! "Edward" "Olmos" 61)))
;; (person-first-name p) ;; is "Edward"
;; (person-age p) ;; is 62
;; p) ;; returns (person! "Edward" "Olmos" 61)
Message not available