Emacs Lisp Quiz: replace-digits-by-subscript

Emacs Lisp Quiz: replace-digits-by-subscript

Here's a interesting question. I have this elisp functon:

(defun replace-digits-by-subscript (string)
  "Replace digits by Unicode subscript characters in STRING.
For example, 「103 and 42」 ⇒ 「₁₀₃ and ₄₂」."
  (let ((myStr string))
    (setq myStr (replace-regexp-in-string "0" "₀" myStr))
    (setq myStr (replace-regexp-in-string "1" "₁" myStr))
    (setq myStr (replace-regexp-in-string "2" "₂" myStr))
    (setq myStr (replace-regexp-in-string "3" "₃" myStr))
    (setq myStr (replace-regexp-in-string "4" "₄" myStr))
    (setq myStr (replace-regexp-in-string "5" "₅" myStr))
    (setq myStr (replace-regexp-in-string "6" "₆" myStr))
    (setq myStr (replace-regexp-in-string "7" "₇" myStr))
    (setq myStr (replace-regexp-in-string "8" "₈" myStr))
    (setq myStr (replace-regexp-in-string "9" "₉" myStr))
    myStr
    ))

You might think it's a bit verbose, or inefficient. But i can't think of way to improve it. Can you come up with a better version?

See also: Semantics of Symbols: Use of Unicode Subscript Digit Characters @ http://xahlee.blogspot.com/2011/10/semantics-of-symbols-use-of-unicode.html

2011-10-18 See solutions at http://xahlee.org/emacs/elisp_replace_subscript.html

Popular posts from this blog

11 Years of Writing About Emacs

does md5 creates more randomness?

Google Code shutting down, future of ErgoEmacs