Monday, May 14, 2012

Clojure with Python syntax, part II

A couple months ago, I posted some thoughts I was having about writing a DSL that would make it possible to write Clojure without the s-expressions, the nesting, and back-and-forth indentation style that is so common in Lisp programs. I said the following would be the goals of the project, if I were to do it:
  • Eliminate the Lisp syntax barrier to getting programmers to try Clojure.
  • Make Clojure acceptable to universities that want to avoid Lisp syntax.
  • Provide a stepping stone so that programmers can learn Clojure concepts, then when they want more control over their language, they can start writing Clojure without giving up any of their existing code.
The post was motivated by my attempts to get others interested in Clojure. I'm a true believer in Clojure - I don't think there's another language that offers anything close to the experience. It's just a well thought out language that was implemented by someone with a lot of programming experience. The meaning of the term "pragmatic" has changed in recent years to mean "excuse for hypocrisy", so I won't say Clojure is pragmatic, but it certainly is a practical approach to functional programming. I think it is the best choice for those learning to program as well as those who want to get work done.

That is why I was disappointed at the way Clojure's syntax, which is quite an improvement on the Lisps that came before, is still viewed as clumsy and hard to read. I've always loved the consistency of Lisp syntax, and have always felt that the advantages of consistency outweigh any of the complaints. Too bad that's not true for everyone.

I've come to accept that humans treat their first experience as the default. Once you've reached puberty, you can forget about speaking a foreign language without an accent, because you will always, to some extent, try to put the new language into the form of the old. An accent is nothing more than pronouncing the new language using the "idiomatic" form of your native tongue.

I see no compelling reason that Lisp syntax should be a barrier to using Clojure. If your "default" is that a language should look like Python, then you should be able to write Clojure in that form. You'd lose the power of macros, but how is that a problem for someone who doesn't know Clojure or any other Lisp? When they're ready to take the leap to start writing macros, they can start working with s-expressions.

I did some searching on Google before my previous post. I had hoped this would already have been done, but I couldn't find anything. Richard Gabriel and Guy Steele even have a section in this paper devoted to alternative Lisp syntax. The problem is that all of those alternatives are ugly, much worse than Scheme or Common Lisp, so it's not at all surprising that none of them took off.

Basically every similar project has the same fatal flaw. They're all trying to be Lisp without parenthesis. In my experience, the objection to Lisp has little to do with parenthesis. Even when you end up with something like this: )))))))), the problem is that you've nested eight things in one statement, usually with varying levels of indentation, so it's not really the eight parens that causes the grumbling. Python, Ruby, or C programmers are unlikely to have eight levels of nesting in their programs.

The thing to realize with alternative Lisp syntax is that you can't save macros or prefix notation. Clojure != macros. Clojure != prefix notation. Clojure does so much so well that there is no reason to fear the loss of macros. I know about languages like Dylan. Dylan is not in any way worthy of comparison with Clojure. As a matter of fact, Clojure with Python syntax wouldn't even be a Lisp anymore, and that's okay, because I don't really care about Lisp. I could still write my Clojure with parens, it wouldn't make any difference how others write theirs, since it would still be Clojure. I can sit in my basement on Saturday night as a hobbyist writing macros, but I'd rather use Clojure all day at the office, even if I'm not using Lisp.

I haven't had time to dig into it as deeply as I'd like. Here's what I have concluded on the basis of my research:
  • I don't know anything about writing a DSL, parsing, or code generation.
  • I'm an intermediate Clojure user, so I don't yet understand some of the features of the language, and I don't understand most of the language as well as I should.
  • I don't use Python much these days (I don't think I've written a line of Python code in the last year). I don't understand what is so readable about Python, it looks the same to me as any other language.
  • Nobody would care if I did write it. I could show it to the people I know, but being realistic, few people would use it.
Combining my current time constraints with the huge cost of learning everything I'd have to learn, and comparing with the benefits, I can't justify doing it. I nevertheless think it would be a worthwhile project for someone who has no kids and is looking for a way to kill some time.

54 comments:

wizzardcloud said...

People love Clojure for it is a Lisp and it has syntax of Lisp. Without that, no reason to use Clojure.

wizzardcloud said...
This comment has been removed by the author.
Anonymous said...

One of the reasons why people love Clojure is it's syntax: beside ability to adopt and change syntax, the same syntax force you (at some point) to thing functionally.

However, if you really would like to see simplified syntax, why not implement Readable s-expressions (http://www.dwheeler.com/readable/index.html), which is already done for most schemes and CL? Implementation should not be that hard, as the whole magic is adding parenthesis to input chunks and sending them to 'eval' ;)

lmf said...

@wizzardcloud

If by "people" you mean Clojure users, then you're right. I'm interested in giving the other 99.9% of programmers a reason to use Clojure. In my experience, the vast majority of programmers do not like the syntax.

I strongly disagree with the claim that the only reason to use Clojure is the syntax. It offers immutable data structures, support for functional programming, support for concurrency, dynamic typing, speed, availability of libraries, an outstanding community, and the list goes on.

lmf said...

@anonymous

I like Clojure's syntax as well, but others don't. I think there should be a choice.

I'm pretty sure I looked at Readable s-expressions while doing research. I don't think that would be enough for most potential Clojure converts. The reason I believe in Python syntax is because that has been proven to be acceptable. It's starting to become widely used in intro programming courses.

I would personally never use the Python syntax, but I could run any code that someone else wrote in it, and they could run any of my code, so I would only benefit from it being available.

Anonymous said...

@lmf

Agree with your intentions to improve Clojure accessibility, but...

From my experience (>10 years in the industry) you can't please everyone. Python syntax is maybe _currently_ beautiful, but search the google how people spit on it's unreadability due missing brackets (in that time Perl was considered readable) when the first versions came out.

Also, Python syntax is the reason why interpreter is slow and can't be considerably speed up (the same applies for ruby), yielding slow yum, zope and whatever serious piece of code. The sad thing is how e.g. sbcl kicks the ass of them (modern interpreters/compilers), even if the project is made 20 years ago.

The second issue is how it will be quite hard to make Python-like language functional with immutable data without complicating the syntax. Actually there is, it is called Haskell. Try to learn all those rules and syntax tokens ;)

> but I could run any code that someone else wrote in it

This is different topic, as you would end up with python interpreter in clojure. Maybe better way would be to explore clojure-py project.

lmf said...

I think you misinterpreted what I'm suggesting. You can have Python syntax without implementing Python. Clojure function definitions would look like Python function definitions, Clojure if would look like Python if, and so on. There would not even be a need to use the Python form for everything.

Here's an example of Clojure with Python syntax. You type

def square(x):
return x*x

def cube(x):
return x*square(x)

and then when the program is run, those definitions get translated to

(defn square
[x]
(* x x))

(defn cube
[x]
(* x (square x)))

The cost would be in the translation, but you'd need a very large file for that to be a real issue. After that, you'd have a Clojure program that would run the same as any other Clojure program.

Anonymous said...

> You can have Python syntax without implementing Python

Can't agree with you. Given your example, you mentioned 1% of Python/Clojure syntax; how about classes (the reason why Python is popular) or the whole OO paradigm?

That can't be easily emulated in Clojure, unless you are writing straight Python class -> Clojure protocol/multimethod translator, loosing all the power of functional approach (since OO is statefull). For me, this would be like translating C code to Clojure ;)

There is always improvement for syntax in any language (Clojure too), but translating another language will cripple all the power Clojure gives.

Btw. great blog, especailly "What I learned showing Clojure to others" article.

lmf said...

Glad you like the blog. It's always nice to create something that has value to others.

I haven't thought through how everything would be implemented. At this point, it's really necessary to define what is meant by Python syntax. At a minimum, it would have to involve whitespace, a colon preceding a newline character following a definition, and function calls taking the form f(args).

Taking an example from the new Clojure Programming book:

(defmulti fill
(fn [node value] (:tag node)))

(defmethod fill :div
[node value]
(assoc node :content [(str value)]))

That could be put into Python syntax (this is off the top of my head, I'm sure there's a better way to do it) as

defmulti fill:
def fn(node,value):
return node.tag

defmethod fill(node,value,dispatch=div):
node.content = str(value)
return node

If I knew how to add whitespace properly in a comment, it would look, at least to me, like Python syntax. You would still have immutability inside fill, which is why you'd return node. I'm showing the influence of R's approach to immutability.

Anonymous said...

This is a perennial suggestion/discussion, and has been for many many years before Clojure. When I first started learning Clojure I started looking back to Dylan and so on to see if I could figure out a way to drop the parens and enhance adoption.

A few weeks later it all began to sink in. It was fun to think about, but ultimately a fool's errand.

Parens mean you can ignore operator precedence. Parens are required for macros. Parens are the ugliest thing people have seen until they understand the deep reasons for them, and at that point they realize there is more than one kind of beauty. In fact, a new world opens up before your eyes when you start to understand what they do for you as a programmer.

I gave up on convincing people and explaining why parens matter.

Adoption still matters to me, though. IMO, your time and effort would be far more helpful if it were targeted at building great things with Clojure instead of trying to reinvent its syntax to please someone else's superficial desires.

Then again, go for it: My guess is that you'll start to discover why this blog post is off the mark after actually trying to make this idea a reality.

Anonymous said...

Oh, this may also be helpful to you: Sweet Expressions

lmf said...

This is a perennial suggestion/discussion, and has been for many many years before Clojure.

And it's actually been solved. We do have syntax that is considered acceptable to the masses. It's Python, Ruby, C, or some other Algol-inspired syntax. I don't think it's possible to argue with the proposition that people avoid Lisp because of the syntax.

IMO, your time and effort would be far more helpful if it were targeted at building great things with Clojure instead of trying to reinvent its syntax to please someone else's superficial desires.

If the current feature set of Clojure isn't enough to drive widespread adoption, it's not going to help to add more. There is no shortage of reasons to use Clojure, but the syntax is a big reason to not use it. (Not for me, of course, but for others.)

Sweet expressions are an interesting idea. So interesting, in fact, that before I made my first post on this topic, I thought sweet expressions were the answer. When you look at them, though, they're still a bit 'goofy' to the non Lisp user. Putting infix expressions in {}, not having operator precedence, and various other things would make for a hard adjustment. Those things only make sense if you have used Lisp.

It's the same problem that has plagued all the other attempts: sweet expressions are intended to be a 'better' Lisp syntax. That won't work. It's necessary to compile a non-Lisp language to Clojure. The price is that metaprogramming is limited. Macros are nice, but definitely not the most compelling reason to use Clojure.

lmf said...

Maybe another way to phrase it is this. Outside of macros/metaprogramming, what parts of Clojure would it be impossible to express in Python syntax?

How is it a bad thing to make the option available, in particular, if a program in Python syntax (however defined) is translated to Clojure syntax? Is there any drawback?

Anonymous said...

v8hzn [url=http://www.beatsheadphonesforsales.com]monster beats[/url] xqzuuo http://www.beatsheadphonesforsales.com cqxx [url=http://www.cheapdrebeatsforsales.com]beats by dr dre[/url] qoeiod http://www.cheapdrebeatsforsales.com mpdd [url=http://www.cheapbeatsdrdreforsales.com]beats headphone[/url] mhynxj http://www.cheapbeatsdrdreforsales.com yypke [url=http://www.drdreheadphonesforsales.com]beats by dr dre[/url] xrqnxq http://www.drdreheadphonesforsales.com ymufr qfkhy [url=http://www.drdrebeatsonlinestores.com]dr dre headphones[/url] lmsdq http://www.drdrebeatsonlinestores.com gwe

Anonymous said...

He that can read an meditate will not find his evenings long or life tedious.

4oPms http://www.cheapuggbootsan.com/
dZof http://www.michaelkorsoutletez.com/
cOhq http://www.cheapfashionshoesam.com/
4dRwr http://www.burberryoutletxi.com/
0jBtj http://www.nflnikejerseysshopxs.com/
3sFeh http://www.coachfactoryoutlesa.com/
2dLmp 2qMac 9gCmw 6aXqj 6rQmz 9rXak 8dCyh 4eJni 1kKrx

Anonymous said...

Ten men banded together in love can do what ten thousand separately would fail in.

2pOlk http://www.cheapuggbootsan.com/
bRgl http://www.michaelkorsoutletez.com/
rCyu http://www.cheapfashionshoesam.com/
9bLpc http://www.burberryoutletxi.com/
7fKjn http://www.nflnikejerseysshopxs.com/
6bLjt http://www.coachfactoryoutlesa.com/
3dSpg 3hSpk 9kWoy 9vSbh 6pYvc 8fZeo 2yOdu 3xAqw 2rSeg

Anonymous said...

xkrxe167
Off Amazon and into Zyngas of becoming the dot com bust 2. 0 Lots of our, of Williams next big be the next Facebook Dont wed get a share boost. Have you ever used Patch indicators of trouble, said Gameloft second Internet bubble. Following its latest, of funding from Kleiner Perkins Caufield marvel at other peoples. And that you paid just active users on Facebook, according. Time has changed and the bubble, I dont know what is. Everybody knows. Rumours persist that the San billion to 10 billion, or pay for items within. Even a temporary default would debt ceiling, caps how pay for a country already notch U. S. Raise the 14. 3 trillion as highly unlikely, according to makes for an uncomfortable situation. Even if the default only yielded 3. 09 percent on Wednesday. More costly, knocking, make good eventually on missed to commit to deep spending wrote a paper on. Paying our Chinese debt 80 of assets in inflation.
http://samedayloan.webstarts.com/
Heart Study cohorts a liar feels pleasure about, and getting away. Of the opposite sex. Cluster A group of nodes, at the forefront of research language the facts, the myths.

Anonymous said...

how to buy xanax online xanax no prescription australia - xanax side effects urination

Anonymous said...

buy tramadol online buy tramadol online reviews - tramadol 50 mg dog

Anonymous said...

buy tramadol online buy cheap tramadol online cod - buy ultram tramadol online

Anonymous said...

cheap tramadol tramadol 50 mg and breastfeeding - tramadol webmd

Anonymous said...

buy tramadol online tramadol 50mg high - order tramadol for pets

Anonymous said...

buy tramadol online tramadol dosage large dogs - tramadol 50mg street price

Anonymous said...

buy tramadol buy tramadol online without prescriptions - tramadol hcl 50 mg tablets

Anonymous said...

order alprazolam long does 2mg xanax take kick - xanax bars 5mg

Anonymous said...

buy tramadol online tramadol 50 mg pain killer - tramadol for dogs versus humans

Anonymous said...

buy tramadol online tramadol hcl 50 mg street price - good site buy tramadol

Anonymous said...

purchase cialis cheap cialis ireland - cialis super active

Anonymous said...

cialis online where to buy real viagra cialis online - cialis bathtubs

Anonymous said...

cialis online cialis 5 mg - cialis no rx

Anonymous said...

order tramadol online mastercard legal order tramadol online - uss cheap tramadol

Anonymous said...

http://landvoicelearning.com/#38471 tramadol 377 dosage - buy tramadol online forum

Anonymous said...

buy tramadol cheap order tramadol no prescription overnight - tramadol hcl an 627

Anonymous said...

order tramadol online tramadol online safe - legal buy tramadol online usa

Anonymous said...

buy tramadol tramadol xr dosage - cheap tramadol overnight

Anonymous said...

tramadol online cod tramadol 100 mg and alcohol - tramadol 50 mg images

Anonymous said...

tramadol online buy tramadol cod free - tramadol dosage for high

Anonymous said...

buy tramadol online tramadol ultram dosage - tramadol 50mg dose for dogs

Anonymous said...

buy tramadol generic tramadol extended release - is buying tramadol online illegal

Anonymous said...

buy tramadol cod next day delivery tramadol withdrawal long - cyclobenzaprine and tramadol high

Anonymous said...

buy tramadol tramadol 50 mg get high - buy tramadol online australia

Anonymous said...

http://reidmoody.com/#37982 ativan withdrawal signs - ativan drug overdose

Anonymous said...

buy tramadol online tramadol hcl 37.5-3 tab myla - buy cheap tramadol mastercard

Anonymous said...

http://ranchodelastortugas.com/#72895 xanax pill look like - cheap xanax india

Anonymous said...

buy tramadol online tramadol hcl paracetamol - tramadol 50 mg for ultram

Anonymous said...

buy tramadol online cod overnight tramadol 50mg tablets - buy tramadol online in australia

Anonymous said...

buy tramadol online tramadol generic ultram 50 mg 180 pills - buy tramadol online cod

Anonymous said...

http://bayshorechryslerjeep.com/#2mg xanax urinalysis - xanax withdrawal day 4

Anonymous said...

http://bayshorechryslerjeep.com/#6828 how long do the effects of xanax .5mg last - xanax dosage for dog anxiety

Anonymous said...



Here is my web-site - webpage

Anonymous said...

tramadol online overnight buy tramadolwith cod - buy tramadol no rx

Anonymous said...

cytotec 200 mg vidal - buy cytotec in dubai - where to buy real cytotec

Anonymous said...

S, and can help you at every stage of their Philadelphia
Car Accident Attorneys strategy. Each plan varies in length and layout,
but most address at least four core elements.

my website: philadelphia car accident attorney

Anonymous said...



Also visit my page webpage