Difference between Scala REPL and Clojure REPL - Compilation Speed

I tried to run two factorial functions with the same algorithm, one in Scala, the other in Clojure:

// Scala: def factorial(n:Int) = (1 to n).foldLeft(1: BigInt)(_*_) 

-

 ;; Clojure: (defn factorial [x] (reduce * (range 1N (inc x)))) 

The first time I enter the REPL function, Clojure evaluates (defining the function without calculating the factorial) without any noticeable delay; while scala just stopped for a short time. (Although very, very short, is still noticeable.)

When I use the function to calculate the factorial, both results come back very quickly.

I would like a basic understanding of REPL. Is there a difference between two REPLs? Is scala REPL a real REPL?

+5
source share
1 answer

A REPL has a rather specific meaning. "True REPL" will match the following pattern: Read Eval Print Loop. You can build REPL in clojure with just a few lines:

 (loop [] (let [string (read-line) data (read-string line) result (eval data)] (println result) (recur))) 

Here you see the main parts of true repl. read-line reads text from the console. read-string converts this string to data (lists, vectors, numbers, etc.). eval evaluates the data returning the result, and println prints the result.

Some argue (and I agree) that only those systems that follow these four steps can be called repl. And some would also point out that Scala is not homo-conic, and therefore cannot have truly repl.

By homoiconic, I mean that the compiler works with the same data structures that are created by the language reader, and is controlled by the language core constructs. For example, this is a perfectly valid clojure code:

  (eval (list (symbol "+") 41 1))) ; evals to 42 

So, the gist of the discussion on the β€œreal” REPL. Only homoiconic languages, such as lisp (and perhaps prologue?), Can have true REPL. Everyone else should really be called "interactive translators."

As for the speed. This is probably due to the complexity of the compiler. The clojure compiler is only about 10 thousand lines of fairly linear code. Single passage, nothing special. The Scala compiler is pretty advanced, supporting things like static typing and multiple passes. These additional features are not needed in a language such as Clojure, and they tend to slow down the compiler.

+10
source

All Articles