
Clojure & Python, Side by Side - llambda
http://tin.nu/sudoku.html
======
swannodette
Side by side assumes that you would write the Clojure solution this way.

But that's not how I (nor, I imagine, any experienced Lisper) would do it.
Lisp programmers are not afraid to write compilers in Lisp targeting Lisp.
This is not something a Python programmer would consider doing.

I would implement Dan Friedman et al's Scheme constraint solver (cKanren) in
Clojure (which can be done in a few hours) so I could solve all sorts of
problems, not just Sudoku, in 80 lines of code instead of 200 with very good
performance.

~~~
asmala
Great, I was wondering what would be an idiomatic way of solving this in
Clojure.

Given the addition of core.logic into Clojure 1.3.0, one could even skip the
implementation of cKanren. Although to be honest I'm not altogether sure how
core.logic measures up to cKanren.

~~~
swannodette
I'm the main developer of core.logic :) core.logic is simply missing cKanren
constraint solving extensions at the moment, but again they won't be hard to
add since core.logic is a fairly faithful interpretation of miniKanren (the
system which cKanren extends).

------
storborg
I love this format. Can anyone point to more "Rosetta Stone" examples like
this? (Aside from ever-prevalent "X in Coffeescript compiles to Y in
Javascript")

~~~
rw
In 30 languages: <http://rosettacode.org/wiki/Sudoku>

------
skylan_q
This is pretty cool.

I know python well enough to hack my way around it. I'm probably missing
something, but now I have to ask: what would be the point in me learning
clojure?

~~~
dkersten
Clojure has excellent concurrency support.

Clojure has a focus on functional programming, immutable data[1] and the
sequence abstraction[2].

Clojure is a Lisp and has most[3] of the super-powerful metaprogramming
facilities that you would expect from a Lisp. This means you can, with enough
effort, mold the language to suit your needs, even turning it into a
completely different language should you wish to do so.

Clojure is not object oriented, though it does allow you to instantiate and
create Java objects for the purpose of interacting with Java libraries.
Clojure does support OO-like code through its Records/Protocol system.

Clojure has out-of-the-box support for multimethods.

Clojure runs on the JVM and therefore has access to the many many libraries
written in Java or other JVM languages.

Pure Clojure code is, as far as I can tell, higher performance than pure
CPython code.

PS: Just for the record, I love Python and use it a lot (and have actually
been out of touch with Clojure for almost nine months now, sadly). The above
are reasons why you may want to learn Clojure - not reasons to drop Python.

[1] The benefits on pure-functional code and immutable data are that it makes
concurrency easier and safer and that it makes code easier to reason about as
you do not need to consider that 1) side-effects are happening behind your
back and 2) the data is being changed elsewhere (which is also why it makes
concurrency easier).

[2] Similar idea to Pythons iterables, except IMHO Clojure's sequences are
much more a part of the core language than iterables in Python, in that in
Clojure most things are sequences, while in Python this isn't the case (though
a _lot_ of things are iterables).

[3] I say most because Clojure does _not_ have reader macros

~~~
zurn
"Pure Clojure code is, as far as I can tell, higher performance than pure
CPython code."

Not so here; the author mentions that the Python version is faster (in his
blog post).

~~~
AlexBaranosky
Adding type hints to the relevant spots in the Clojure code might have helped
him in terms of performance.

------
jphackworth
This is really neat. Although, one thing that makes it easier to map this
program from Python to Clojure is that the original program was written in a
pretty functional style. It would be interesting to see if a more imperative
sample program, or one using more object-oriented features, would map
similarly.

~~~
phreakhead
I agree. For some things, like more mathy things that only perform one
function, it seems like functional programming is the way to go. However, for
HUGE programs based on data and complex interactions between and extensions of
objects, it seems like a more object-oriented style would be better... all
about the right tool for the job, I guess.

~~~
asmala
I think you're absolutely right that functional programming is particularly
suited for things like mathematics. But you can also enjoy many of the
benefits of a functional approach by moving most of the complexity into purely
functional, well, functions and leaving the state-modifying code fairly
simple. For a good pragmatic example of this approach, check out the snake
game in Programming Clojure v2 from Pragmatic Programmers.

Coming from Ruby this approach was a bit mind-boggling at first, but even with
my fairly limited Clojure exposure (or should that be expojure?) I've come to
appreciate how easy pure functions are to test and to reason about.

------
phreakhead
Python looks like a lot less typing.

~~~
cageface
And much more readable, without the parenthesis balancing dance.

~~~
AlexBaranosky
Just like English is soooo much more readable than German, because well, I
don't know German :)

~~~
ShardPhoenix
I found the Python more readable even though I have written programs in
Clojure and never written programs in Python. In fact I'd be willing to bet
that Python-like syntax would be found to be more readable than Lisp-like
syntax for programmers with N years of experience in that language, if such a
study could be conducted. (ie, I'd bet that an average programmer with 5 years
of Python experience and nothing else can correctly read programs faster than
an average programmer with 5 years of Clojure experience and nothing else (not
that the latter exists)).

------
rmnoon
In addition to performance metrics, this site has implementations of their
benchmarks in all the languages they test.

It's not pretty and side-by-side, but it's fascinating.

<http://shootout.alioth.debian.org/>

~~~
igouy
Make it side-by-side :-)

<http://shootout.alioth.debian.org/help.php#twowindows>

------
timf
See the previous discussion (about Justin's blog post):
<http://news.ycombinator.com/item?id=2386575>

------
protez
LISP is the coolest language ever and maybe forever, in theory. In practice,
it also works phenomenal, but just for 1%.

~~~
viandante
Meh, I am an amateur when it comes to programming and still I am loving
clojure over python. Truth is that when you are using lists all the time, lisp
seems just more natural. All those for and if in object oriented languages
feel pretty awkward when you manipulate lists and hashes.

Just an amateur opinion although.

------
amalag
Would love to see this in Ruby

~~~
oacgnol
Is there a site out there that has common implementations of problems using
different languages? I would love to be able to compare.

~~~
karavelov
[http://rosettacode.org/wiki/Category:Solutions_by_Programmin...](http://rosettacode.org/wiki/Category:Solutions_by_Programming_Task)

------
danbmil99
The parentheses -- my eyes -- pain

------
phatbyte
I can only admire the persistance (free time) of someone coding in Clojure.
Everytime someone says to me "I hate Objective-C syntax" I show them
Clojure...

If I want fast performance I code in C or other low level language. Sorry
Clojure community, I'm not drinking the kool aid this time.

~~~
jwr
What's really unique about the Clojure community is that (as opposed to many
other language communities) we don't respond to vague handwaving-type
flamebaits such as this one.

Most of us just get stuff done using Clojure, quickly, getting concise and
most importantly — correct code, even in massively multithreaded applications.

