This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Rearranging Word - Attempt 2

By: Da_Gamer on Apr 28th, 2013  |  syntax: Scheme  |  size: 1.60 KB  |  views: 65  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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.   (create-new-words a-1str a-word empty))
  31.  
  32. ; 1-str word word -> list-of-word
  33. ; creates new combinations of word
  34. (define (create-new-words l w str-w)
  35.   (cond
  36.     [(empty? w) (list (cons l str-w))]
  37.     [else (cons (create-new-word (move-letters str-w (first w))
  38.                                  l
  39.                                  (rest w))
  40.                 (create-new-words l (rest w) (move-letters str-w (first w))))]))
  41.  
  42. ; word 1-str word -> word
  43. ; combines two word and 1-str to form new word
  44. (define (create-new-word l1 ins-ltr l2)
  45.   (append l1 (list ins-ltr) l2))
  46.  
  47. ; word 1-str -> word
  48. ; appends 1-str to word
  49. (define (move-letters w1 l1)
  50.   (cond
  51.     [(empty? w1) (cons l1 w1)]
  52.     [else (append w1 (list l1))]))
clone this paste RAW Paste Data