我需要从字符串中删除字符,但我无法弄清楚如何指定count
,这是对character
应该从{{string
删除的次数的限制在鸡肉中1}}
以下是我在Common Lisp中的表现:
(let ((a "abca")) (delete #\a a :count 1))
;=> "bca"
我怎么会在鸡计划中这样做?
我尝试在Chicken中使用delete
,但鸡delete
似乎不支持count
这件事,所以以下内容不工作:
(let ((a "abca")) (list->string (delete #\a (string->list a) :count 1)) ))
只需在字符串上运行delete
即可删除所有char
:
(let ((a "abca")) (list->string (delete #\a (string->list a)) ))
;=> "bc"
答案 0 :(得分:1)
(define myfilter
(lambda (lst char cnt)
(if (null? lst)
'()
(if (and (eq? (car lst) char) (> cnt 0))
(myfilter (cdr lst) char (- cnt 1))
(cons (car lst) (myfilter (cdr lst) char cnt))))))
(define delnchars
(lambda (str char count)
(list->string (myfilter (string->list str) char count))))
> (delnchars "3 i's will be removed, the last -> is here" #\i 3)
"3 's wll be removed, the last -> s here"