1 / 2
Feb 2016

I've found what purports to be a solution to the TEST problem (Life, The Universe, and Everything) in Clojure, but it fails to produce any output. I'm just trying to get something to work in Clojure so I can move on to other problems but as of now I'm frustrated and disappointed. The code I tried is:

(ns spoj-test) 

(defn read-int 
  [] 
  (let [s (read-line)] 
    (Integer/parseInt s))) 

(defn main 
  [] (let [n (read-int)] 
       (when (not (== 42 n)) 
         (println n) 
         (recur))))

which fails because (according to SPOJ) it produces no output. When I run this in a Clojure REPL it seems to work fine. Any idea where I've gone wrong? I've also tried running this without the (ns spoj-test), thinking that it might make the main function un-findable, but got the same results. I also tried changing the first line to

(ns clojure.main)

but this did not affect the results either.

  • created

    Feb '16
  • last reply

    Feb '16
  • 1

    reply

  • 1.3k

    views

  • 1

    user

  • 1

    like

OK, problem solved. I had it in my head that SPOJ was doing something to compile the submitted code to a class file which was then loaded and that this would execute the 'main' function automatically.

This turns out to be incorrect.

What I was missing was a separate line (form, in Lisp/Clojure terms) which invoked 'main' after it was defined; e.g.

(main)

That's all it took. I discovered this when I went back and reviewed the code I'd originally found - it had the separate invocation of main but I'd managed to miss it when I copied the code.

So, a little misunderstanding on my part led to a couple of wasted days and a fair amount of frustration. Well, I'm only 58 - maybe when I'm all grown up I'll understand this stuff better.

Oh, that's right - I'm already all grown up - guess I won't... smile