Anonymous user
Polymorphic copy: Difference between revisions
polymorphic sequence copy in common lisp
(CL (for structures)) |
(polymorphic sequence copy in common lisp) |
||
Line 142:
Sub has foo = FOO, bar = BAR.
eq: NIL; eql: NIL; equal: NIL; equalp: T
NIL</pre>
===With Sequences===
The same technique works for <code>[http://www.lispworks.com/documentation/HyperSpec/Body/t_seq.htm sequence]</code> and its subclasses (e.g., <code>[http://www.lispworks.com/documentation/HyperSpec/Body/t_string.htm string]</code>, <code>[http://www.lispworks.com/documentation/HyperSpec/Body/t_seq.htm list]</code>) when <code>[http://www.lispworks.com/documentation/HyperSpec/Body/f_cp_seq.htm copy-seq]</code> is used rather than <code>copy-structure</code>.
<lang lisp>(defmethod frob ((sequence sequence))
(format t "~&sequence has ~w elements" (length sequence)))
(defmethod frob ((string string))
(format t "~&the string has ~w elements" (length string)))</lang>
<pre>> (let* ((hw1 "hello world")
(hw2 (copy-seq hw1)))
(frob hw1)
(frob hw2)
(format t "~&eq: ~w; eql: ~w; equal: ~w; equalp: ~w"
(eq hw1 hw2) (eql hw1 hw2)
(equal hw1 hw2) (equalp hw1 hw2)))
the string has 11 elements
the string has 11 elements
eq: NIL; eql: NIL; equal: T; equalp: T
NIL</pre>
|