removeText
From: David B. Ellis (dellis_at_randallpub.com)
Date: 09/30/04
- Next message: Tim Bradshaw: "Re: Sacla loop: a new loop implementation"
- Previous message: Alexander Schmolck: "Re: [ANN] Sacla loop: a new loop implementation"
- Next in thread: Jeff M.: "Re: removeText"
- Reply: Jeff M.: "Re: removeText"
- Reply: Kenny Tilton: "Re: removeText"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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!
- Next message: Tim Bradshaw: "Re: Sacla loop: a new loop implementation"
- Previous message: Alexander Schmolck: "Re: [ANN] Sacla loop: a new loop implementation"
- Next in thread: Jeff M.: "Re: removeText"
- Reply: Jeff M.: "Re: removeText"
- Reply: Kenny Tilton: "Re: removeText"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|