【问题标题】:LISP- Delete vowels from a stringLISP-从字符串中删除元音
【发布时间】:2013-04-02 08:51:11
【问题描述】:

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

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

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

(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))

【问题讨论】:

  • 只需使用REMOVE-IF
  • 那么您的意思是遍历字符串并删除-if vowelp?
  • (defun delete_vowels (word) (let (str (string word)) (end (- (length str) 1)) ) (remove-if #'vowel-p str end) ) (defun vowel-p (char) (find char "aeiou" :test #'char-equal))我做错了什么?

标签: string lisp common-lisp


【解决方案1】:
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"

【讨论】:

  • 如果我将 'remove-if' 放在一个由我定义名称的函数中,那会是什么样子?所以我只会调用 (deletevowels "spectacular") 它会给我 "spctclr"。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-23
  • 2021-05-23
  • 2014-06-04
  • 1970-01-01
  • 2018-08-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多