Hi ! On Friday, April 8, 2016 at 3:28:55 PM UTC+2, Olivier Scalbert wrote: > > Hello everybody ! > > I just start learning Clojure and I am a complete newbie ! > > I have tried to solve this classical small puzzle with Clojure. > Assign one digit from {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} to each letter {o n e > t h r l v w y} so that > one + one + one + three + three + eleven = twenty > […] > How can I change the code into a more idiomatic one ? > > For what it's worth, this can be done easily with the great loco library [loco] : ;; straight port of cf https://github.com/aengelberg/loco/blob/master/test/loco/integer/sendmoremoney1.clj ;; (:use loco.constraints ;; loco.core))
(defn initialize-digits [vars] (for [v vars] ($in v 0 9))) (def letters [:o :n :e :t :h :r :l :v :w :y]) (def puzzle-model (concat (initialize-digits letters) [($distinct letters) ($> :o 0) ($> :t 0) ($> :e 0) ($= ($+ ($* 3 ($scalar [:o :n :e] [100 10 1])) ($* 2 ($scalar [:t :h :r :e :e] [10000 1000 100 10 1])) ($scalar [:e :l :e :v :e :n] [100000 10000 1000 100 10 1])) ($scalar [:t :w :e :n :t :y] [100000 10000 1000 100 10 1]))])) (time (solutions puzzle-model)) "Elapsed time: 61.496745 msecs" ({:y 8, :r 7, :v 1, :o 2, :n 3, :w 4, :e 5, :l 0, :h 9, :t 6}) Cheers, bernard [loco] https://github.com/aengelberg/loco -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.