我正在尝试获取用户输入并将其存储在一个列表中,只是我希望每个扫描的单词都是它自己的字符串,而不是一个由单个字符串组成的列表。 示例:
> (input)
This is my input. Hopefully this works
会返回:
("this" "is" "my" "input" "hopefully" "this" "works")
请注意,我不希望在最终列表中有任何空格或标点符号。
任何输入将不胜感激。
最佳答案
split-sequence
是现成的解决方案。
您也可以自己滚动:
(defun my-split (string &key (delimiterp #'delimiterp))
(loop :for beg = (position-if-not delimiterp string)
:then (position-if-not delimiterp string :start (1+ end))
:for end = (and beg (position-if delimiterp string :start beg))
:when beg :collect (subseq string beg end)
:while end))
其中 delimiterp
检查您是否要拆分此字符,例如
(defun delimiterp (c) (or (char= c #\Space) (char= c #\,)))
或
(defun delimiterp (c) (position c " ,.;/"))
附言。查看您的预期返回值,您似乎想调用 string-downcase
在 my-split
之前。
PPS。您可以轻松修改 my-split
以接受 :start
、:end
、:delimiterp
&c.
PPPS。对于 my-split
的前两个版本中的错误,我们深表歉意。请考虑一个指标,即人们应该不推出自己的此功能版本,而应使用现成的解决方案。
关于string - Lisp - 将输入拆分为单独的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15393797/