Reputation: 51
(ns factorials)
(defn displayFactorials[number]
(if (> number 1)
(do
(* number (displayFactorials(- number 1))))))
(defn -main []
(println "To display the factorials,")
(print " enter the number: ") (flush)
(let
[ number (double (read))]
(println)
(displayFactorials number)
(println)
)
)
I'm writing a factorial program that receives a user input. The program is compiling, bu the result is not printing. Any suggestions on what I should do?
Upvotes: 0
Views: 93
Reputation: 29966
You need to make it look like this:
(println (displayFactorials number))
Here is a better formatted version of the program:
(ns demo.core)
(defn displayFactorials
[number]
(if (> number 1)
(* number (displayFactorials (- number 1)))))
(defn -main []
(println "To display the factorials,")
(print " enter the number: ") (flush)
(let [number (double (read))]
(println
(displayFactorials number))))
Hint: You also forgot about what to do if the number is equal to one.
Update
See the hint above. Write a unit test in the file test/tst/demo/core.clj
like:
(ns tst.demo.core
(:use demo.core tupelo.test))
(deftest dummy#
(println :answer (displayFactorials 5)))
Try running lein test
and see the exception:
ERROR in (dummy#) (Numbers.java:1068)
Uncaught exception, not in assertion.
expected: nil
actual: java.lang.NullPointerException: null
at clojure.lang.Numbers.ops (Numbers.java:1068)
clojure.lang.Numbers.multiply (Numbers.java:173)
demo.core$displayFactorials.invokeStatic (core.clj:6)
demo.core$displayFactorials.invoke (core.clj:3)
demo.core$displayFactorials.invokeStatic (core.clj:6)
demo.core$displayFactorials.invoke (core.clj:3)
demo.core$displayFactorials.invokeStatic (core.clj:6)
demo.core$displayFactorials.invoke (core.clj:3)
demo.core$displayFactorials.invokeStatic (core.clj:6)
demo.core$displayFactorials.invoke (core.clj:3)
tst.demo.core$fn__18527.invokeStatic (core.clj:11)
tst.demo.core/fn (core.clj:10)
that should be a BIG clue as to what is missing.
Upvotes: 1