Hi,
I’m just starting to learn Clojure. Most of my (limited) programming experience is in shell scripting. In trying to write something that will calculate factor pairs, I came across this:
https://gist.github.com/ecmendenhall/5970568
http://ecmendenhall.github.io/blog/
To incorporate that code, I did lein new app factorpairs
and edited factorpairs/src/factorpairs/core.clj to look like so:
(ns factorpairs.core
(:gen-class)
(:use [clojure.core.logic :refer :all])
(:use [clojure.core.logic.fd :refer [in interval eq]]))
(defn factor-pairs [number]
(run* [pair]
(fresh [factor1 factor2]
(in factor1 factor2 (interval 2 number))
(eq (= number (* factor1 factor2)))
(== pair [factor1 factor2]))))
(defn -main [x]
(println (factor-pairs x)))
Also edited factorpairs/project.clj to be:
(defproject factorpairs "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0"
:url "https://www.eclipse.org/legal/epl-2.0/"}
:dependencies [[org.clojure/clojure "1.11.1"]
[org.clojure/core.logic "1.0.1"]]
:repl-options {:init-ns factorpairs.core}
:main factorpairs.core)
But when I try get the factors for a number, say 40:
lein run 40
I see the following:
WARNING: == already refers to: #'clojure.core/== in namespace: factorpairs.core, being replaced by: #'clojure.core.logic/== Execution error (ClassCastException) at clojure.core.logic.fd/interval (fd.clj:372).class java.lang.String cannot be cast to class java.lang.Number (java.lang.String and java.lang.Number are in module java.base of loader 'bootstrap')
If I edit -main
to place the number I wish to factor directly like so:
(defn -main []
(println (factor-pairs 40)))
and do
lein run
It kinda works:
WARNING: == already refers to: #'clojure.core/== in namespace: factorpairs.core, being replaced by: #'clojure.core.logic/==
([2 20] [4 10] [5 8] [8 5] [10 4] [20 2])
But I’d like to be able to pass the number as an argument on the command line.
And I’m not sure what’s going on with that “WARNING: == already refers …” message. Did some searching about but could not sort out how to resolve that.
Thanks for any help / suggestions,
John