Want more features on Pastebin? Sign Up, it's FREE!
Guest

word-breaks

By: mamboking on Aug 12th, 2011  |  syntax: Clojure  |  size: 0.53 KB  |  views: 159  |  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. ;; word-breaks
  2. (def dict #{"a" "apple" "applepie" "base" "baseball"
  3.             "bat" "gift" "i" "pi" "pie" "twist"})
  4.  
  5. (defn permute [ss]
  6.   (distinct
  7.    (mapcat identity
  8.            (let [cnt (count ss)
  9.                  coll (take cnt (iterate rest ss))]
  10.              (for [n (range 1 (inc cnt))]
  11.                (mapcat (fn [x]
  12.                          (map (fn [y] (apply str y))
  13.                               (partition n x)))
  14.                        coll))))))
  15.  
  16. (defn find-word-bounds [ss]
  17.   (filter dict (permute ss)))
clone this paste RAW Paste Data