I remember in the past I wrote a Python program that did the same thing. I remember how the algorithm was considered smart, but now it is trying to implement it from memory in Clojure. I am having some problems.
I'm new to Clojure, so I know that I'm probably not doing it in the best way.
Below I use the word "herps" as a test, and it should return a list of all possible combinations of the word. I finally get the combinations correctly, but they are nested, and I need a flat list of words. I think, because to return lazy seq , but I'm not sure how to get around it.
(ns combos.core (:gen-class)) (use '[clojure.string :only [join]]) (defn rmletter [in letter] (join (remove #(= letter %) in))) (defn combo [total in] (if (= (count in) 1) (concat total (list in)) (for [item in] (do (if (= (count in) 5) (print "top: ")) (combo (concat total (list item)) (rmletter in item))) ))) (defn -main "I don't do a whole lot ... yet." [& args] ;; work around dangerous default behaviour in Clojure (alter-var-root #'*read-eval* (constantly false)) (doseq [item (combo nil "herps")] (print "item:")(println item)) (println "Hello, World!"))
And here is the conclusion:
top: item:((((herps) (hersp)) ((heprs) (hepsr)) ((hesrp) (h espr))) (((hreps) (hresp)) ((hrpes) (hrpse)) ((hrsep) (h rspe))) (((hpers) (hpesr)) ((hpres) (hprse)) ((hpser) (h psre))) (((hserp) (hsepr)) ((hsrep) (hsrpe)) ((hsper) (h spre)))) top: item:((((ehrps) (ehrsp)) ((ehprs) (ehpsr)) ((ehsrp) (e hspr))) (((erhps) (erhsp)) ((erphs) (erpsh)) ((ershp) (e rsph))) (((ephrs) (ephsr)) ((eprhs) (eprsh)) ((epshr) (e psrh))) (((eshrp) (eshpr)) ((esrhp) (esrph)) ((esphr) (e sprh)))) top: item:((((rheps) (rhesp)) ((rhpes) (rhpse)) ((rhsep) (r hspe))) (((rehps) (rehsp)) ((rephs) (repsh)) ((reshp) (r esph))) (((rphes) (rphse)) ((rpehs) (rpesh)) ((rpshe) (r pseh))) (((rshep) (rshpe)) ((rsehp) (rseph)) ((rsphe) (r speh)))) top: item:((((phers) (phesr)) ((phres) (phrse)) ((phser) (p hsre))) (((pehrs) (pehsr)) ((perhs) (persh)) ((peshr) (p esrh))) (((prhes) (prhse)) ((prehs) (presh)) ((prshe) (p rseh))) (((psher) (pshre)) ((psehr) (pserh)) ((psrhe) (p sreh)))) top: item:((((sherp) (shepr)) ((shrep) (shrpe)) ((shper) (s hpre))) (((sehrp) (sehpr)) ((serhp) (serph)) ((sephr) (s eprh))) (((srhep) (srhpe)) ((srehp) (sreph)) ((srphe) (s rpeh))) (((spher) (sphre)) ((spehr) (sperh)) ((sprhe) (s preh)))) Hello, World!
source share