是否有一種簡單的方法來定義一個函數,當傳遞參數時重複它自己?如何定義一個在傳遞參數時重複自己的函數
舉例來說,我已經定義了以下功能
(defun swap-sign()
(interactive)
(search-forward-regexp "[+-]")
(if (equal (match-string 0) "-")
(replace-match "+")
(replace-match "-"))
)
我想C-u swap-sign
調用swap-sign
四倍。
我已經試過
(defun swap-sign (&optional num)
(interactive)
(let ((counter 0)
(num (if num (string-to-number num) 0)))
(while (<= counter num)
(search-forward-regexp "[+-]")
(if (equal (match-string 0) "-")
(replace-match "+")
(replace-match "-"))
(setq counter (1+ counter)))))
但C-u swap-sign
仍只運行交換號(或者更精確地說,while循環體)一次。我猜測這是因爲if num
不是測試num
是否爲空字符串的正確方法。
我在正確的軌道上,還是有更好/更簡單的方式來延長swap-sign
?
這只是爲了改變標誌。難道你不會只是確定這個數字是否是奇數並且改變一次? – corn3lius 2010-05-11 14:11:43
每次調用'swap-sign'都會使光標指向匹配後的第一個字符。所以四次調用應該交換四個標誌。我把它叫做'1231.196 + 760.489 + 213.553 + 122.165-24.633'這樣的字符串。 – unutbu 2010-05-11 14:20:20