3 ms·
> Python and Lisp coder here. At first look, Lisp is ugly to look at. Maybe for you; Lisp was great for me to look at from the moment I laid eyes on it. Beaut
by kazinator 9y ago
> Python and Lisp coder here. At first look, Lisp is ugly to look at.
Maybe for you; Lisp was great for me to look at from the moment I laid eyes on it.
Beauty is very subjective; but the technical advantages or disadvantages of a syntax aren't.
Some years prior to becoming a Lisp programmer, I had one tiny interaction with Lisp that wasn't terribly good. It was this: some CLOS example code in a book on OOP by Grady Booch. I was trying to follow the syntax and it seemed to have weird repetition in it. That was arising from the defclass syntax for declaring accessors and initforms, like this (foo :initarg :foo) and (bar :accessor bar) type stuff. I was boggled by all the repetitions: why do we have foo and :foo, and so on. I had no clue about Lisp keyword symbols at that time.
I didn't hate the syntax, and it seemed less verbose than C++, for sure. The parentheses in it seemed perfectly fine. It was just boggling a little bit: more so than something like (let ((x 2) (y 3)) (+ x y)), or (loop for x below 10 summing x).
- flavio81 9y ago> I was boggled by all the repetitions: why do we have foo and :foo, and so on. For what it's worth, here is my "defclass-easy" macro which will create a class with the slots you want, it also has a "required-slots" list as a parameter. Slots that are in both "slot-list" and "required-slots" will have an ":initarg" of the same name as the slot, and will raise an error if not supplied when making an object. Enjoy! (defmacro defclass-easy (class-name slot-list required-slots) ;;required-slots should be a list as well `(defclass ,class-name () ,(mapcar (lambda (x) (if (null(member x required-slots)) (list x) (list x :initarg (read-from-string (concatenate 'string ":" (string x))) ;creates :initarg :<slot name> :initform `(error ,(concatenate 'string "initarg required: " (string x)))))) ; (error "initarg required: xxxx") slot-list)))
- kazinator 9y agoIt's interesting to note that Lisp has some "def easys", just not for classes. There is a defsetf for simple situations, so you don't have to use define-setf-expander. defsetf's simplicity isn't one-size-fits-all either; it has a short form and a longer form. In the same area of functionality, there is define-modify-macro which handles simple cases, freeing the user from calling get-setf-expansion and dealing with its return values. P.S. (read-from-string (concatenate 'string ":" (string x))) ==> (intern (string x) 'keyword) `(error ,(concatenate 'string "initarg required: " (string x)) ==> `(error "initarg required: ~s" ',x)