多次从字符串X中删除char

时间:2013-08-05 17:11:35

标签: string chicken-scheme

我需要从字符串中删除字符,但我无法弄清楚如何指定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"

1 个答案:

答案 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"