removeText

From: David B. Ellis (dellis_at_randallpub.com)
Date: 09/30/04


Date: 30 Sep 2004 05:09:19 -0700

I'm learning Lisp. As part of an exercise I'm writing removeText.

I have two questions about it. First, is there a Common Lisp function
that I've overlooked that would do this for me? And second, how would you
write this function?

Here are my tests for removeText:

     (deftest test-removeText-that-never-occurs ()
        (check (equal (removeText "no such" "from this") "from this")))

     (deftest test-removeText-from-beginning ()
        (check (equal (removeText "this " "this text") "text")))

     (deftest test-removeText ()
        (check (equal (removeText "this " "this text has this more times")
                      "text has more times")))

     (deftest test-removeText-at-end ()
        (check (equal (removeText "ending" "this ending")
                      "this ")))

And here's the code:

     (defun removeText (aTextToRemove aText)
        (let
           ((aResultText "")
            (aTextLength (length aText))
            (aSearchLength (length aTextToRemove)))

           (do
              ((anIndex 0 (1+ anIndex))
               (aSearchLimit (1+ (- aTextLength aSearchLength))))

              ((equal anIndex aTextLength) aResultText)

              (setq aNewText (subseq aText anIndex (1+ anIndex)))
              (if (< anIndex aSearchLimit)
                 (if (equal aTextToRemove
                            (subseq aText anIndex (+ anIndex aSearchLength)))
                    (progn
                       (setq anIndex (1- (+ anIndex aSearchLength)))
                       (setq aNewText ""))))
         
              (setq aResultText
                 (concatenate 'string
                    aResultText
                    aNewText)))))

Thanks!



Relevant Pages

  • Re: removeText
    ... >> I'm learning Lisp. ... As part of an exercise I'm writing removeText. ... is there a Common Lisp function ... That is a big ditto for DO but that is a personal opinion rather than a ...
    (comp.lang.lisp)
  • Re: removeText
    ... As part of an exercise I'm writing removeText. ... is there a Common Lisp function ...
    (comp.lang.lisp)