Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Common Lisp
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====Defining generic functions and methods==== The macro <code>defgeneric</code> defines [[generic function]]s. Generic functions are a collection of [[Method (computer programming)|methods]]. The macro <code>defmethod</code> defines methods. Methods can specialize their parameters over CLOS ''standard classes'', ''system classes'', ''structure classes'' or individual objects. For many types, there are corresponding ''system classes''. When a generic function is called, [[multiple dispatch|multiple-dispatch]] will determine the effective method to use. <syntaxhighlight lang="lisp"> (defgeneric add (a b)) </syntaxhighlight> <syntaxhighlight lang="lisp"> (defmethod add ((a number) (b number)) (+ a b)) </syntaxhighlight> <syntaxhighlight lang="lisp"> (defmethod add ((a vector) (b number)) (map 'vector (lambda (n) (+ n b)) a)) </syntaxhighlight> <syntaxhighlight lang="lisp"> (defmethod add ((a vector) (b vector)) (map 'vector #'+ a b)) </syntaxhighlight> <syntaxhighlight lang="lisp"> (defmethod add ((a string) (b string)) (concatenate 'string a b)) </syntaxhighlight> <syntaxhighlight lang="lisp"> (add 2 3) ; returns 5 (add #(1 2 3 4) 7) ; returns #(8 9 10 11) (add #(1 2 3 4) #(4 3 2 1)) ; returns #(5 5 5 5) (add "COMMON " "LISP") ; returns "COMMON LISP" </syntaxhighlight> Generic Functions are also a [[First-class citizen|first class data type]]. There are many more features to Generic Functions and Methods than described above.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)