Ken Tilton wrote:
>
Paul Rubin wrote:
>Ken Tilton <ke*******@gmai l.comwrites:
>>Have you read On Lisp by Paul Graham? It is on-line. Just the preface
will do, I think, maybe also Chapter One where he raves on macros. Do
you think he is mistaken? Confused? Lying? Mutant?
I remember Paul Graham's piece about macros that made him sound like
someone who went nuts with them, as is certainly possible to do.
But you could have just flipped thru the rest of the pages to see if he
/had/ gone nuts with them!
> In
my experience, good coders write for clarity and that includes in
their use of Lisp macros. All in all Lisp's macro system is something
like the C preprocessor. Yes, you can obfuscate things horribly with
it, but you can also use it to make things clearer, and that's what
good programmers do.
One has to be from the US and of a certain age to remember this, but
this old line from a commercial for high-octane gasoline says it for me:
power to be used, not abused.
Here is a bit of concrete I just tossed off:
(defmacro defskill (id &body skill-info)
`(progn
,@(loop with sub-id = id
for (q . q-def) in skill-info
collecting
(ecase q
((title annotations hints)
`(defmethod ,(intern (conc$ 'skill- q)) ((tf-id (eql
',sub-id)))
,@q-def))))))
It lets me code this:
(defskill absolute-value
(title "Absolute Value")
(annotations
"Take the absolute value of #op#."
"The vertical bars around #op# mean 'the absolute value of' #op#."
"Absolute value of #strn# is the 'distance' of #strn# from zero."
"Absolute value is always zero or positive: #str|n|=n#, and
#str|-n|=n#.")
(hints
"What do those vertical bars around #op# mean?"
"Have you learned about 'absolute value'?"
"Absolute value can be thought of as the 'distance' of a value from
zero on the number line, and distance is always positive."
"The rule is:#str|-n|=|n|##str=n#. Can you apply that to #op#?"
"Some examples: #str|+42|=42#, #str|-42|=42#, and #str|0|=0#."
"To get the absolute value of a number such as #op#, we simply drop
any minus sign."))
....and get this:
(PROGN
(DEFMETHOD SKILL-TITLE ((TF-ID (EQL 'ABSOLUTE-VALUE)))
"Absolute Value")
(DEFMETHOD SKILL-ANNOTATIONS ((TF-ID (EQL 'ABSOLUTE-VALUE)))
"Take the absolute value of #op#." "The vertical bars around #op#
mean 'the absolute value of' #op#."
"Absolute value of #strn# is the 'distance' of #strn# from zero."
"Absolute value is always zero or positive: #strn=n#, and #str-n=n#.")
(DEFMETHOD SKILL-HINTS ((TF-ID (EQL 'ABSOLUTE-VALUE)))
"What do those vertical bars around #op# mean?" "Have you learned
about 'absolute value'?"
"Absolute value can be thought of as the 'distance' of a value
from zero on the number line, and distance is always positive."
"The rule is:#str-n=n##str=n#. Can you apply that to #op#?" "Some
examples: #str+42=42#, #str-42=42#, and #str0=0#."
"To get the absolute value of a number such as #op#, we simply
drop any minus sign."))
The above is how my upcoming death-defying interactive Algebra
tutor-in-a-drum ("You'll laugh! You'll cry!") will know how to coax some
befuddled teen through |-42| -42 if they get stuck. And a hundred
other subskills (so there will be a hundred of these definitions).
If I hire someone they will look at defskill and not fall to the ground
frothing at the mouth. They likely will not even macroexpand it because
it is self-explanatory. If they wonder if there are other options they
can alt-. to the source. The language has been extended, but I followed
a pattern familiar to Lispniks. Zero confusion results.
If I decide not to use generic method dispatch to "look up" the hints I
just have to change the macro and then write a DEFUN (no dispatch on
type) to, say, look up the symbol in a hash table.
hth,ken
ps. I won't mention the other benefit, which is that I want educators to
edit these if they have a better idea on how to tutor absolute value. I
am not mentioning it because I am as impatient with superfluous syntax
as a non-programmer would be. k
pps. How would Python do this? Is it possible to avoid committing to an
implementation mechanism? Compare and contrast. k
--
Algebra:
http://www.tilton-technology.com/LispNycAlgebra1.htm
"Well, I've wrestled with reality for thirty-five
years, Doctor, and I'm happy to state I finally
won out over it." -- Elwood P. Dowd
"I'll say I'm losing my grip, and it feels terrific."
-- Smiling husband to scowling wife, New Yorker cartoon