clojure

Mutually recursive definitions in Clojure

How do I do mutually recursive definitions in Clojure? Here is a code in Scala to find prime numbers which uses recursive definitions: val odds: Stream[Int] = cons(3, odds map { _ + 2 }) val primes: Stream[Int] = cons(2, odds filter isPrime) def primeDivisors(n: Int) = primes takeWhile { _ <= Math.ceil(Math.sqrt(n))} filter { n % _...

clojure/lein REPL with jline

For some reason I can't get clojure REPL working with jline, what i did was git clone the clojure repository off github then run ant to build it, then i download jline-0.9.94.jar to the directory with clojure.jar, then run the following command: java -cp jline-0.9.94.jar:clojure.jar jline.ConsoleRunner clojure.main And get the followi...

Stuck in a Clojure loop, need some guidance

Hi, I am stuck in a Clojure loop and need help to get out. I first want to define a vector (def lawl [1 2 3 4 5]) I do (get lawl 0) And get "1" in return. Now, I want a loop that get each number in the vector, so I do: (loop [i 0] (if (< i (count lawl)) (get lawl i) (recur (inc i)))) In my mind this is supposed...

common lisp cons creates a list from two symbols, clojure cons requires a seq to cons onto?

(Disclaimer - I'm aware of the significance of Seqs in Clojure) In common lisp the cons function can be used to combine two symbols into a list: (def s 'x) (def l 'y) (cons s l) In clojure - you can only cons onto a sequence - cons hasn't been extended to work with two symbols. So you have to write: (def s 'x) (def l 'y) (cons s '(l...

Single character console input in java/clojure

How can I read a single character/key from the console without having to hit Enter? There is an old entry in Sun's bug database claiming that it can't be done in pure java. I've found these approaches JNI JLine [http://jline.sourceforge.net/] Javacurses [http://sourceforge.net/projects/javacurses/] I'd expect to add a single magic-...

Defining Clojure -main function in IntelliJ

I need some very basic advice on how to define a working -main function in IntelliJ along the lines of: (ns clojure.examples.hello (:gen-class)) (defn -main [greetee] (println (str "Hello " greetee "!"))) When I create a project, paste the preceding code in a source file, and set the run configuration (with Script path, modul...

Dynamic let List Destructuring in Clojure

I have a let statement in which I would like to dynamically destructure a list. The following is my solution: symList ;; list of some Strings which will become the vector of Symbols to assign to valList ;; list of some values, same length as symList (let [(map read-string symList) valList] ...) An example value of symList would b...

Using C-style encapsulation techniques in Clojure?

I am working on my first (non-trivial) Clojure program I don't really feel comfortable with how I am declaring all my mutable state globally. For example: (def next-blocks (atom [])) (def num-next-blocks 1) (def is-game-over (atom false)) (def user-name (atom (str))) (def hs-xml (atom nil)) Since I use C a lot at work I came up with t...

In Clojure: Error executing a Java call with let inside a function but not in REPL

I have the following code: (defn post [title content timestamp] (let [[innholdet tajm] [(str "<html> <head> <title>" title " :: " blog_title "</title></head> <body><h1>" title "</h1> <br/>" content "<br/><i>posted " (Date. timestamp) "</i> <br/><a href=\"...

Reload Clojure files in emacs

I am just starting out learning Clojure and Emacs. I have got Clojure Box for windows running and I would like to be able to write code in a buffer then run it in the REPL without haveing to call (use 'example.code) all the time. I know about C-c C-k but it doesn't reload the namespace. If i use (in-ns 'example.code) to change nam...

idiomatic way to replace (null x) function from common lisp in clojure

In Common Lisp you use the (null x) function to check for empty lists and nil values. Most logically this maps to (or (nil? x) (= '() x)) In clojure. Can someone suggest a more idiomatic way to do it in Clojure? ...

Troubles Importing Clojure Libs in Paradise

I occasionally get this problem, and generally work around it, but it's rather frustrating. I have all of Incanter (check it out if you don't know it: it's superb) on my classpath. I try to import it (through a Slime REPL) like this: user> (use 'incanter.core), but fail. Doing this: user> (use 'clojure.contrib.def) works just fine, a...

Problem with iterating over a time-series in clojure.

Hi there, I have the following problem: I have a time-series with more than 10000 entries and I want to perform some calculations with each of them. This alone wouldn't be a problem, but I need to get the last calculated value in order to get the next one. A very simple form of what I need would look like this: Val(n) = Val(n-1) + (tim...

Fixed point combinators for functions over custom types?

Most examples of the use of fixed point combinators involve functions that take integers to integers (e.g. factorial). In many cases the fixed point of a function over the real numbers will end up being an arbitrary rational or perhaps irrational number (a famous example is the logistic map http://en.wikipedia.org/wiki/Logistic_map). In ...

Fixed point combinator usage? Why a stack overflow here?

I am confused about something. I wanted to generate an example (in Clojure) demonstrating how a fixed point combinator could be used to evaluate the fixed point of a sequence that mathematically converges after an infinite number of applications but would, in fact, converge after a finite number of steps due to finite precision of floati...

FileNotFoundException when making a jar file from the clojure file

I am trying to go through the process of creating a jar file from a simple clojure file. Below is my clojure code: (ns app.first (:gen-class)) (refer 'clojure.core) (defn -main [& args] (println "this program worked!")) I am using these instructions to create the jar file: http://en.wikibooks.org/wiki/Clojure_Programming/Tutoria...

How Are Lazy Sequences Implemented in Clojure?

I like Clojure. One thing that bothers me about the language is that I don't know how lazy sequences are implemented, or how they work. I know that lazy sequences only evaluate the items in the sequence that are asked for, how does it do this? What makes lazy sequences so efficient that they don't consume much stack? How come you can ...

Why do I get NPE in the following code?

The following code executes as expected but gives a NullPointerException at the end. What am I doing wrong here? (ns my-first-macro) (defmacro exec-all [& commands] (map (fn [c] `(println "Code: " '~c "\t=>\tResult: " ~c)) commands)) (exec-all (cons 2 [4 5 6]) ({:k 3 :m 8} :k) (conj [4 5 \d] \e \f)) ; Output: ; Clojure 1.2.0-...

Test whether a list contains a specific value in Clojure

What is the best way to test whether a list contains a given value in Clojure? In particular, the behaviour of contains? is currently confusing me: (contains? '(100 101 102) 101) => false I could obviously write a simple function to traverse the list and test for equality, but there must surely be a standard way to do this? ...

Parsing command-line arguments from a STRING in Clojure

I'm in a situation where I need to parse arguments from a string in the same way that they would be parsed if provided on the command-line to a Java/Clojure application. For example, I need to turn "foo \"bar baz\" 'fooy barish' foo" into ("foo" "bar baz" "fooy barish" "foo"). I'm curious if there is a way to use the parser that Java o...