#:g1: closでL-99 (P21 指定した位置に要素を挿入する)

Posted 2008-05-10 10:28:00 GMT

-(http://www.ic.unicamp.br/~meidanis/courses/mc336/2006s2/funcional/L-99_Ninety-Nine_Lisp_Problems.html)
call-next-methodすると色々できそうなんだけど、乱発すると何だか良く分からないことになるなあ…。

(insert-at 'alfa '(a b c d) 2)
;=> (A ALFA C D)

(insert-at 'alfa #(a b c d) 2) ;=> #(A ALFA C D)

(insert-at 'alfa "abcd" 2) ;=> "aALFAcd"

(defgeneric INSERT-AT (item sequence position) (:documentation "P21 (*) Insert an element at a given position into a list."))

(defmethod INSERT-AT (item (sequence array) (position integer)) (call-next-method (if (stringp sequence) (to-string item) (vector item)) sequence position))

(defmethod INSERT-AT (item (sequence list) (position integer)) (call-next-method (list item) sequence position))

(defmethod INSERT-AT (item (sequence sequence) (position integer)) (concatenate (class-of sequence) (subseq sequence 0 (1- position)) item (subseq sequence position)))

(defgeneric TO-STRING (obj) (:documentation "coerce to string."))

(defmethod TO-STRING ((obj symbol)) (string obj))

(defmethod TO-STRING ((obj string)) obj)

(defmethod TO-STRING (obj) (write-to-string obj))


comments powered by Disqus