有人可以向我解释一下我如何搜索句子中每个单词的出现次数,例如 Common lisp 中的“the cat sat on the mat”吗?
用户必须事先输入这行文本,然后必须从中计算出现次数。任何关于从哪里开始的帮助都会有所帮助。
最佳答案
看看 The Common Lisp Cookbook 中的 split-by-one-space
(defun split-by-one-space (string)
"Returns a list of substrings of string
divided by ONE space each.
Note: Two consecutive spaces will be seen as
if there were an empty string between them."
(loop for i = 0 then (1+ j)
as j = (position #\Space string :start i)
collect (subseq string i j)
while j))
现在您可以只使用带有 equal
的哈希表作为测试,并可能使用列表中的 mapc
来获取键和频率哈希。然后你在哈希表中就有了你想要的东西。
或者(更慢)是用 (count (car lst) lst :test #'equal)
来计算第一个元素,然后用 (remove ( car lst) lst :test #'equal)
.当过滤列表为空时,您就完成了。
关于LISP:查找句子中每个单词的出现次数。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29413880/