1

I have just begun learning clojure and I'm using the Textmate editor for writing the scripts. However, I am not able to figure out how to run it from the terminal. Like I type the clj filename.clj command but nothing happens. Do I need to include the function name also somewhere because I have a function that takes a number as an argument.

Here is my code that I want to run from the terminal:

(defn next-collatz-num [n]


(if (even? n)
    (quot n 2)
    (inc (* n 3))))

(defn collatz [n]
  (take-while #(< 1 %)(iterate next-collatz-num n)))

(defn max-count-collatz [n]
  (when (> n 0)
    (first
      (reduce
        #(if (> (last %1)(last %2)) %1 %2)
          [1 1] (map #(list % (count (collatz %))) (range 1 (inc n)))))))

(max-count-collatz 999999)

2 Answers 2

2

Clojure has a much more interactive environment than just running a whole script at the terminal command prompt.

TL;DR, install leiningen, create a project.clj, then run lean repl.

If you don't want to create a project.clj, or if you're curious how to do it the hard way, read on...

You can start a Clojure read-eval-print-loop (REPL) interactive prompt with

java -cp clojure-1.6.0.jar clojure.main

(download the latest Clojure jar here).

Once you're in the REPL, load the code file:

(load-file "my-script.clj")

Now, you can call the function directly:

(max-count-collatz 5)

If it doesn't work as you'd expect, change the code, save and reload it in the REPL:

(require 'my-script :reload-all)
Sign up to request clarification or add additional context in comments.

8 Comments

I don't understand the statement "Once you are in the REPL". How?
@EricaMaine, use the first line (java -cp clojure-1.6.0.jar clojure.main) at the terminal to launch the Clojure REPL.
Exception in thread "main" java.lang.NoClassDefFoundError: clojure/main This is what I get when I use that line.
@EricaMaine this assumes clojure-1.6.0.jar is in the current working directory. If it's not, adjust the path to clojure-1.6.0.jar accordingly. See clojure.org/getting_started for more details.
It's best to Install leiningen. Then just run lein repl
|
2

While it is possible to run individual Clojure files using Clojure.jar, one of the best things about Clojure is the leiningen dependency manager and build tool. Creating a project is easy, and for anything more than a single file with no external dependencies, it is a huge improvement over using java and Clojure.jar directly.

3 Comments

Hi, in my code in the question, when I type (collatz 7), the result that comes is (7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2), but it should actually be (7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1). Why is it missing the 1 at the end?
that's because you call (take-while #(< 1 %) ...) - perhaps you want pos? or (complement zero?) or #(> % 1)
Hi, I was trying to work with namespaces this time in my clojure code, using ns. So, I have my jar file in the Downloads directory, I created a folder "poker" in the Downloads directory. Inside the "poker folder, I created two clojure files that are dependent on each other, so I also put my jar file inside the poker folder. Now, when I did: java -cp clojure-1.6.0.jar clojure.main It gave me "Permission denied". Why is that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.