suggestions for better string mangling
Posted: Sun Sep 21, 2008 9:03 am
I wrote this yesterday, converting it as best I could from Python:
The idea was to take a word and to generate a set of most of the possible single-letter modifications of that word. So given "newlisp", you'd get deletions: "ewlisp" "nwlisp" "nelisp"..., transpositions: "enwlisp" "nwelisp" ..., insertions: "anewlisp" "bnewlisp" ..., and alterations: "aewlisp" "bewlisp"....
Lutz said I was using 'string casts' - I think he meant those functions; they're to stop replace modifying the original word too soon. But I'm sure there's a better way of doing the job. Anyone got any bright ideas?
Code: Select all
(define (edits1 word)
(let ((l (length word)) (res '()))
(for (i 0 (- l 1)) (push (replace (nth i (string word)) (string word) "") res -1)) ; deletion
(for (i 0 (- l 2)) (push (swap i (+ i 1) (string word)) res -1)) ; transposition
(for (i 0 (- l 1)) (for (c (char "a") (char "z"))
(push (replace (nth i (string word)) (string word) (char c)) res -1) ; alteration
(push (replace (nth i (string word)) (string word) (string (char c) (nth i (string word)) )) res -1))) ; insertion
res))
Lutz said I was using 'string casts' - I think he meant those
Code: Select all
(string word)