LISP-从字符串中删除元音

时间:2013-04-02 08:51:11

标签: string lisp common-lisp

我想编写一个删除字符串中所有元音的函数。我想过定义一个检测元音的函数,类似于symbolp,zerop等,如果它是元音,则删除它。我怎样才能做到这一点?我将不胜感激。感谢

(defun deletevowels (string)
(go through the list
   (if vowel-p deletevowels )
)
)

尽管如此,我有一个问题,如果它是最后一个删除元音,我如何修改它以满足我想要做的,删除字符串中的所有元音?在下面的代码中,我提到了这个函数,vowel-p one。

(defun strip-vowel (word)
  "Strip off a trailing vowel from a string."
  (let* ((str (string word))
         (end (- (length str) 1)))
    (if (vowel-p (char str end))
        (subseq str 0 end)
        str)))

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

此外,如果我使用下面的函数将字符串转换为列表然后在列表中循环而不是字符串来查找元音并将其删除,是否更容易?

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

1 个答案:

答案 0 :(得分:2)

CL-USER 27 > (defun vowel-p (char) (find char "aeiou" :test #'char-equal))
VOWEL-P

CL-USER 28 > (remove-if #'vowel-p "abcdef")
"bcdf"

请参阅:Common Lisp Hyperspec,REMOVE-IF

CL-USER 29 > (defun deletevowels (string) (remove-if #'vowel-p string))
DELETEVOWELS

CL-USER 30 > (deletevowels "spectacular")
"spctclr"