Lispには文字列置換関数がない?
いろいろ探してみたが、Lispには文字列置換関数がないようだ。指定した範囲の文字列を置換できる、replaceという関数はある。
The Common Lisp Cookbook - Stringsにreplaceの使用例がある。
> (setf my-string (string "Zeppo Marx")) "Zeppo Marx" > (replace my-string "Harpo" :end1 5) "Harpo Marx"
これを応用して、replace-allという関数の実装例も紹介されている。(quote from The Common Lisp Cookbook - Strings)
(defun replace-all (string part replacement &key (test #'char=)) "Returns a new string in which all the occurences of the part is replaced with replacement." (with-output-to-string (out) (loop with part-length = (length part) for old-pos = 0 then (+ pos part-length) for pos = (search part string :start2 old-pos :test test) do (write-string string out :start old-pos :end (or pos (length string))) when pos do (write-string replacement out) while pos)))
文字列の置換は、基本的な関数のような気がするが定義されていないのか・・・Lispでは文字列を置換するようなケースはない、ということなのだろうか。標準関数的に組み込んであってもいい気がするけれどなぁ。Javaのように、オモチャみたいにいろいろなことができる関数は用意されていないのかもしれない。