0

我必须编写一个程序,将字符串的元音、辅音和其他符号分别更改为 C、V 0。我已经这样做了,但我想知道是否有更有效和更优雅的方法来做到这一点。将不胜感激输入。

(defun string-to-list (string)
(loop for char across string collect char))

(defun is-vowel (char) (find char "aeiou" :test #'char-equal))

(defun is-consonant (char) (find char "bcdfghjklmnpqrstvwxyz" :test #'char-equal))

(defun letter-type (char)
(if (is-vowel char) "V"
(if (is-consonant char) "C"
"0")))

(defun analyze-word (word-string)
(loop for char across word-string collect (letter-type char)))

此外,我想把它变成一个字符串,我该怎么做?我应该定义一个遍历列表并使其成为字符串的函数还是更简单的方法?

4

2 回答 2

2
(defun letter-type (char)
  (cond ((find char "aeiou" :test #'char-equal) #\V)
        ((alpha-char-p char) #\C)
        (t #\0)))

CL-USER> (map 'string #'letter-type "analyze-word")
"VCVCCCV0CVCC"
于 2013-04-02T14:15:19.087 回答
0

只是为了这个想法:

(defun multi-replace-if (sequence function &rest more-functions)
  (map (type-of sequence)
       (lambda (x)
         (loop for f in (cons function more-functions)
            for result = (funcall f x)
            while (eql x result)
            finally (return result)))
       sequence))

(multi-replace-if "bcdfghjklmnpqrstvwxyz"
                  (lambda (x) (if (find x "aeiouy") #\v x))
                  (lambda (y) (declare (ignore y)) #\c))
"cccccccccccccccccccvc"
于 2013-04-02T20:56:25.397 回答