Advertisement
Da_Gamer

Rearranging Word - Attempt 1

Apr 28th, 2013
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 2.04 KB | None | 0 0
  1. ; 1-str is a one-character-string from the set [A-Z] or [a-z]
  2.  
  3. ; Word is either:
  4. ; empty
  5. ; (cons 1-str Word)
  6.  
  7. ; List-of-word is either:
  8. ; (cons Word empty)
  9. ; (cons Word (List-of-word))
  10.  
  11. ; word -> list-of word
  12. ; returns all possible combinations of word
  13. (define (arrangements w)
  14.   (cond
  15.     [(empty? w) (list empty)]
  16.     [else (insert-everywhere/in-all-words (first w)
  17.             (arrangements (rest w)))]))
  18.  
  19. ; 1str list-of word -> list-of word
  20. ; inserts 1-str into all positions of all words in list-of-word
  21. (define (insert-everywhere/in-all-words a-1str a-low)
  22.   (cond
  23.     [(empty? a-low) a-low]
  24.     [else (append (insert-everywhere/in-one-word a-1str (first a-low))
  25.                   (insert-everywhere/in-all-words a-1str (rest a-low)))]))
  26.  
  27. ; 1str word -> list-of word
  28. ; inserts 1-str into all positions in word
  29. (define (insert-everywhere/in-one-word a-1str a-word)
  30.   (cond
  31.     [(empty? a-word) (list (list a-1str))]
  32.     [else (create-combinations (first a-word)
  33.                                (insert-everywhere/in-one-word a-1str (rest a-word)))]))
  34.  
  35. ; 1-str list-of-word -> list-of-word
  36. ; create combinations of word in list-of-word
  37. (define (create-combinations ins-ltr a-low)
  38.   (cond
  39.     [(empty? a-low) a-low]
  40.     [else (append (create-new-words ins-ltr (first a-low) empty)
  41.                   (create-combinations ins-ltr (rest a-low)))]))
  42.  
  43. ; 1-str word word -> list-of-word
  44. ; creates new combinations of word
  45. (define (create-new-words l w str-w)
  46.   (cond
  47.     [(empty? w) (list (cons l str-w))]
  48.     [else (cons (create-new-word (move-letters str-w (first w))
  49.                                  l
  50.                                  (rest w))
  51.                 (create-new-words l (rest w) (move-letters str-w (first w))))]))
  52.  
  53. ; word 1-str word -> word
  54. ; combines two word and 1-str to form new word
  55. (define (create-new-word l1 ins-ltr l2)
  56.   (append l1 (list ins-ltr) l2))
  57.  
  58. ; word 1-str -> word
  59. ; appends 1-str to word
  60. (define (move-letters w1 l1)
  61.   (cond
  62.     [(empty? w1) (cons l1 w1)]
  63.     [else (append w1 (list l1))]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement