Python syntax in Lisp and Scheme
Ingvar Mattsson
ingvar at cathouse.bofh.se
Thu Oct 9 07:16:09 EDT 2003
cstacy at dtpq.com (Christopher C. Stacy) writes:
> >>>>> On Wed, 08 Oct 2003 16:51:44 -0400, Joe Marshall ("Joe") writes:
> Joe> "Carlo v. Dango" <oest at soetu.eu> writes:
>
> >> method overloading,
>
> Joe> Now I'm *really* confused. I thought method overloading involved
> Joe> having a method do something different depending on the type of
> Joe> arguments presented to it. CLOS certainly does that.
>
> He probably means "operator overloading" -- in languages where
> there is a difference between built-in operators and functions,
> their OOP features let them put methods on things like "+".
>
> Lisp doesn't let you do that, because it turns out to be a bad idea.
> When you go reading someone's program, what you really want is for
> the standard operators to be doing the standard and completely
> understood thing.
Though if one *really* wants to have +, -, * and / as generic
functions, I imagine one can use something along the lines of:
(defpackage "GENERIC-ARITHMETIC"
(:shadow "+" "-" "/" "*")
(:use "COMMON-LISP"))
(in-package "GENERIC-ARITHMETIC")
(defgeneric arithmetic-identity (op arg))
(defmacro defarithmetic (op)
(let ((two-arg
(intern (concatenate 'string "TWO-ARG-" (symbol-name op))
"GENERIC-ARITHMETIC"))
(cl-op (find-symbol (symbol-name op) "COMMON-LISP")))
`(progn
(defun ,op (&rest args)
(cond ((null args) (arithmetic-identity ,op nil))
((null (cdr args))
(,two-arg (arithmetic-identity ,op (car args))
(car args)))
(t (reduce (function ,two-arg)
(cdr args)
:initial-value (car args)))))
(defgeneric ,two-arg (arg1 arg2))
(defmethod ,two-arg ((arg1 number) (arg2 (number)))
(,cl-op arg1 arg2)))))
Now, I have (because I am lazy) left out definitions of the generic
function ARITHMETIC-IDENTITY (general idea, when fed an operator and
NIL, it returns the most generic identity, when fed an operator and an
argument, it can return a value that is more suitable) and there's
probably errors in the code, too.
But, in principle, that should be enough of a framework to build from,
I think.
//Ingvar
--
My posts are fair game for anybody who wants to distribute the countless
pearls of wisdom sprinkled in them, as long as I'm attributed.
-- Martin Wisse, in a.f.p
More information about the Python-list
mailing list