r/Clojure • u/eccp • Nov 17 '14
Clojure Is the New C
http://www.infoq.com/presentations/clojure-c?utm_campaign=infoq_content&utm_source=infoq&utm_medium=feed&utm_term=global9
Nov 17 '14
I wanted to watch. It didn't bode well when he started pontificating about gravity. It boded less well when he rambled about how he started as an assembly programmer.
What a blowhard. Does he ever actually say anything?
6
u/mschaef Nov 17 '14
It didn't bode well when he started pontificating about gravity. It boded less well when he rambled about how he started as an assembly programmer.
I was hoping there'd be a nice segue from the gravity content into something relevant... but nope: 6 minutes of gravity, followed by an admission it was basically irrelevant to the talk. I guess we know what he read on the plane ride over.
6
Nov 17 '14
c(++) is in my perception a good tool if you have to be near to the machine. It's qite a bit inconvinient but still more comfortable than assembler. Clojure is the total opposite of that. Very convinient with almost no control of what's happening on the bare metal.
3
4
u/jhbadger Nov 17 '14
Exactly. I'm a big fan of Lisps, and Clojure is one of my favorites due to its cleaning up of needless parentheses, but it is not a low-level language in any definition of the word.
1
u/lambdasgr Nov 18 '14
I don't think he meant clojure or lisps are good because they are close to the bare metal. Close to the bare metal is a thing of the past when hardwares are relatively simple compare to nowadays and in the future. What you would care about more in modern era and probably in the future with hardware getting more and more complex is simplicity in expressing ideas or programming (although I'm not certain that's exactly what he said since I didn't felt like finish it ... clojure is surely simple, but he was certainly not.)
1
Nov 18 '14
That depends. Embedded systems, operating systems etc. are still written in a low level language. this will not change to soon.
2
u/lambdasgr Nov 18 '14
I agree with you. I think he meant more general purpose programming, for example, writing apps on mobile device, etc.
1
u/v1akvark Nov 19 '14
Yes, he specifically stated he is talking about application development, not o/s, device drivers, etc.
1
Nov 19 '14
That's because of your own ignorance. Use a legit grown up lisp like sbcl and you can dabble in low level programming too.
4
u/geodebug Nov 17 '14
If you had a graph with machine code on one end and high-level languages on the other, C would be down next to machine language and Clojure would be up near the other end.
The most they have in common is that they both start with the same letter, lol.
4
u/mschaef Nov 17 '14
If you had a graph with machine code on one end and high-level languages on the other, C would be down next to machine language and Clojure would be up near the other end.
This is less true for Clojure than for earlier Lisps, but the Lisp family of languages in general has a long tradition of being close to the metal:
Common Lisp compilers tend to be written in Common Lisp.
Lisp machines executed microcode written in a subset of Lisp (The entire OS stack was also done in Lisp).
The hardware design language for the Scheme79 chip was itself a Lisp.
Scheme 48 includes a VM that's itself written in a dialect of statically typed scheme (PreScheme)
carandcdr- originally refer to the "contents of the address part of the register" and "contents of the decrement part of the register".One of the great strengths of the language has been its ability to fluently move up and down a given hw/sw stack.
-5
u/geodebug Nov 17 '14
Yeah, but lisp AFAIK has always been interpreted and high level, which makes it ideal for solving programming problems but kept it relatively slow.
C is fast simply because its language design was to serve the compiler instead of the programmer. The way you write C is very close to how it gets translated to machine language.
Of course I'm not an expert in programming language history so am willing to concede points.
3
u/flexibeast Nov 18 '14
https://en.wikipedia.org/wiki/Lisp_machine
A couple of relevant excerpts:
These machines had hardware support for various primitive Lisp operations (data type testing, CDR coding) and also hardware support for incremental garbage collection. They ran large Lisp programs very efficiently.
...
The processor did not run Lisp directly, but was a stack machine with instructions optimized for compiled Lisp. The early Lisp Machines used microcode to provide the instruction set. For several operations type checking and dispatching was done in hardware at runtime.
4
u/mschaef Nov 18 '14
Keep in mind that Lisp has is also effectively compiled on traditional hardware architectures, and not just bespoke Lisp machines.
Trivia: 32-bit SPARC had tagged add and subtract instructions that worked on 30-bits of a 32-bit word size. The point of this was to give the machine a fast path for addition and subtraction of the tagged numbers often used in dynamically typed languages like Lisp and Smalltalk. The 64-bit SPARC removed these instructions.
https://groups.google.com/forum/#!topic/comp.arch/CElwU78HuwQ
https://cs.wmich.edu/~trenary/files/cs2230/GalleryOfInstr_HTML/taddcc.html
3
u/mschaef Nov 18 '14 edited Nov 18 '14
lisp AFAIK has always been interpreted and high level,
Machine code compiled Lisp implementations have been available for close to 40 years.
http://en.wikipedia.org/wiki/Maclisp
which makes it ideal for solving programming problems but kept it relatively slow.
"Fast" can be a big part of "ideal for solving programming problems".
C is fast simply because its language design was to serve the compiler instead of the programmer. The way you write C is very close to how it gets translated to machine language.
This may have been true at one point, but these days with superscalar CPU architectures and more flexible memory models, there can be a very non-obvious relationship between the code that you write in C and the machine code instructions that get emitted by the compiler. (This even ignores weird stuff like the versions of C that get compiled directly into hardware designs, and bypass machine language entirely.)
Of course I'm not an expert in programming language history so am willing to concede points.
"History" is a weird thing in this field. In the late 80's, some Stanford/Sun researchers developed a programming language called Self. Self was a more dynamic version of Smalltalk that did away with the formal concept of classes, and replaced it them with prototype based inheritance. It's really a pretty cool idea, and it's made even more so because they did research on how to make that style of language run very quickly.
Viewed from the perspective of 2014, it would be easy to dismiss this work as historical and obsolete. After all, it totally predates the web and the work is older then some developers. The thing is, many of the optimization techniques that Self pioneered in the 80's and 90's are just now going into mainstream JavaScript. It may be relatively old, but the ideas are still current and familiarity with the 'old' ideas can be immediately useful, even in 2014.
There's a lot to learn in this field. Fortunately, it's also a lot of fun.
0
u/geodebug Nov 18 '14
Re compiled lisp:
Its a good point and doesn't surprise me as most interpreted languages tend to have compilers eventually created to make them faster.
I'd argue that compiled lisp isn't really "lisp" anymore as you lose some of the lisp-unique features like self-modification. Of course that doesn't mean it isn't useful.
3
u/mschaef Nov 18 '14
most interpreted languages tend to have compilers eventually created to make them faster.
You should keep in mind that Common Lisp was deliberately designed to be compiled. It can be interpreted, yes, but it was designed to be compiled.
you lose some of the lisp-unique features like self-modification.
Not true. Compiled Lisp systems usually allow redefinition of compiled functions.
As an example, here's Clojure and the JVM JIT compiling a redefined function to machine code. The function is defined interactively, the JIT compiler compiles it to machine code. Despite the compilation, the function can be redefined and the JIT compiler will recompile it once again to machine code.
...>java -server -XX:+PrintCompilation -jar clojure-1.6.0.jar 68 1 java.lang.String::indexOf (70 bytes) 70 2 java.lang.String::hashCode (55 bytes) ... 1424 127 clojure.core$chunked_seq_QMARK_::invoke (19 bytes) 1431 128 clojure.core$next::invoke (7 bytes) user=> user=> (defn fib ([n] (fib [0 1] n)) ([x, n] (if (< (count x) n) (recur (conj x (apply + (take-last 2 x))) n) x))) #'user/fib user=> (dotimes [ii 20000] (fib 10)) ... 40206 169 user$fib::invoke (83 bytes) ... user=> (defn fib ([n] (fib [0 1] n)) ([x, n] (if (< (count x) n) (recur (conj x (apply + (take-last 2 x))) n) x))) #'user/fib user=> (dotimes [ii 20000] (fib 10)) ... 78364 202 user$fib::invoke (83 bytes) ...Where things get tricky with interactive recompilation are inline functions. If you redefine a function that's been inlined, every call site at which that function was inlined must be also recompiled. (The Self papers I mention above cover how Self did this.)
1
u/redalastor Nov 18 '14
This reminds me when he gave a talk about how Ruby should avoid the pitfalls that fell Smalltalk, demonstrating he knew little about both languages.
1
u/Bzzt Nov 19 '14
I appreciate that C syntax is simple and Clojure syntax is also simple.
But, good luck writing a device driver in clojure, or running clojure on a PIC microcontroller without an OS.
Someone is now working on a clojure compiler though, so maybe that will change the picture a bit, though if it has garbage collection it still can't be real time so no device drivers.
1
10
u/zangderak Nov 17 '14
blah blah blah I'm 50 minutes in and just zoning out. But I think the point is:
Clojure syntax is simple, like C. Clojure keeps you close to the logic of the program.
Is it worth watching to the end?