gpt4 book ai didi

lisp - 从 Nyquist 中的字符串中删除字符

转载 作者:太空宇宙 更新时间:2023-11-03 18:43:13 25 4
gpt4 key购买 nike

如何在 Nyquist(与 xlisp 非常相似)中从字符串中删除某个字符并返回结果?

我想计算像“ABBAAAABBBBAABAAAB”这样的字符串中有多少个“A”。 (是的,字符串中只有 'A' 和 'B'。)

因为 Nyquist 中没有(计数)函数,所以我尝试了类似的方法

(length (remove #\B mystring))

(length (remove #\B mystring :test equal))

但它不起作用。

暂时忘记了字符数,如何从字符串中删除“B”?

最佳答案

字符串中是否总是只有 AB?如果没有,您可能想做类似的事情

(remove #\A yourstring :test-not 'char=)

根据XLISP reference for remove , Nyquist remove 不处理 string,只处理 list。您需要将 string 转换为 list 才能以这种方式对其进行操作,但也没有 coerce。这有点 hacky,但我看到的最简单的解决方法是流式传输 stringread-char。这将生成 charlist,然后您可以使用 remove 对其进行操作。

(defun string->list (a-string)
(let ((collector nil)
(stream (make-string-input-stream a-string)))
(dotimes (c (length a-string) (reverse collector))
(setf collector (cons (read-char stream) collector)))))

现在应该可以

(remove #\A (string->list yourstring) :test-not 'char=)

关于lisp - 从 Nyquist 中的字符串中删除字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9039841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com