76 points · by gnosis · 15y ago · 89 comments
I actually find that Lisp scores some local-readability points over Python in a few cases, mainly because Lisp is more explicit about scope, e.g.
(let ((a (something))
(b (other-thing))
(frobnicate a b))
...other code
versus a = something()
b = other_thing()
frobnicate(a, b)
...other code
is more explicit about the intended lifetimes and consumers of the data "a" and "b". Lisp also has the "with a = (blah)" keyword for "loop" so the assignment can be made specifically in the context of loop preparation.All in all I'm actually happier with my ability to signal my intent (and gauge other programmers' intent) in Lisp than I am in Python.
if 1 < 2:
c = 2
else:
c = 3
print c
(I'm retarded and don't know how to do comments that have new lines in the, but hopefully you catch the drift. The 'print c' is at 0 indentation)What you try to do is not legal Python. The else clause can't go in the same line.
I get your point, though. Python's scope is not in terms of literally 'indentation' blocks. Scope units are: module toplevel, and (possibly nested) function and class definitions. To create a new scope you create a new function. This is what Lisp does too (you can define let as a macro over lambda).
I have oscillated between liking either way better and at present I find Python's sloppier way more practical.
main_scope_stuff()
c = create_context()
for foo in foos:
do_something(foo, c)
do_other_thing(foo, c)
accumulate(foos, c)
more_main_scope_stuff()
to emphasize that c was only important to the loop and the call to "accumulate"? main_scope_stuff()
with create_context() as c:
for foo in foos:
do_something(foo, c)
do_other_thing(foo, c)
accumulate(foos, c)
more_main_scope_stuff()
That signifies that "c" isn't supposed to be available after scope of the "with" statement. The variable will still be bound, but conceptually it's unavailable, since it's not supposed to be used.Also, the object in create_context() needs to support being called like that (i.e. it should contain __enter__ and __exit__ methods).
I'm not really sure what you're trying to show with that code, though. Is your objection that every do_ function is requiring a context?
Edit: I see what the parent was saying now, see bottom comment. Also, see StavrosK's comment if you're fine using Python 2.5 (released 2006).
(f x) alone is a call, a macro or function.
(let ((f x)) ...) , here (f x) is an item in a binding list.
What a Lisp programmer sees is a pattern introduced by LET: LET ((var value)*) body
There are a handful of basic macro code structure patterns. Once they are learned and attached to the symbol of the corresponding macro, the understanding of code is much improved.Reading Lisp code requires to identify the visual marker - the symbol in front. Depending on that there are code patterns that can be visually destructured: lists, property lists, assoc lists, lambda lists, binding lists, ...
In other languages the visual markers are supported by different characters. In Lisp it is a symbol.
{
a := b + x;
}
vs. (progn
(setq a (+ b x)))
As a Lisp programmer I'm trained to see the PROGN, not the parentheses.It is a bit like being afraid of riding a bicycle for the first time. How can one move forward and balance at the same time? Difficult. Once learned it is simple and hard to unlearn.
How can one read the various Lisp code patterns? Once you have learned them it is easy and hard to unlearn.
The only concern that remains is that (f x) could be a function call or a macro invokation. This is entirely by design, and is in fact a core idea of lisp that lets you build your own constructs with the same syntax as the language's native ones.
You know what is truly optimized for writing code and not reading it? Mathematical notation - and for good reason, most mathematical equations are written on paper. Mathematicians use one letter variable names like (i, j, n, m, x, y, z), various Greek letters, infix notation, and other things that often makes mathematical equations incredibly hard to read, but easier to jot down on paper.
Almost all programming languages, such as C, Java, Python, and Perl are influenced by this notation that although it was easy to write on paper, is not as easy to read and not nearly as easy to parse.
Lisp is not nearly as easy to write on paper - and it often is harder to type - however - for any experienced Lisper it is the most readable programming language there is, hands down, because once you use it for long enough the parenthesis fade away and you can write/read code without the mental overhead that is entailed by parsing.
I don't mean to be the next snarky Lisp guy in the room, but I don't see the difference between reading
(if (= a 3) ...)
and thinking "that's a special form and a predicate", and reading if a == 3:
...
and thinking "that's a special form and a predicate". In either case there's "if". Writing "(f x) could be anything" is a straw man. As soon as you replace "f" with "if" the argument falls apart.(No, you can't name a function "if" in Common Lisp. Go ahead, try it.)
guile> (define if (lambda (. args) (display "Haha!") (newline)))
guile> (if (= 3 4) 't 'f)
Haha!:)
Urgh... A hundred times no. I really do appreciate that the author seems to have given Lisp a try. But this is simply wrong.
Why does Lisp provide macros? Because they make code easier to read. I say stop focusing on what language abstraction (f x) represents and focus on figuring out what the code is doing.
Oh well, at least he didn't complain about lisp having too many parenthesis.
Humans are visual creatures. It really is easy to get lost in Lisp's parentheses.
Languages whose blocks are easier to identify at a glance really are easier to read.
The attitude of far too many Lisp developers seems to be that Lisp has failed to dominate after all these many decades because people are just too stupid to realize how great Lisp is.
If Lisp hasn't dominated by now, rather than arrogance and superiority, it would be better to think about why Lisp continues to fail to dominate.
By the way, I'm not suggesting you're one of those people.
Which is why conventions for indenting Lisp code to show block structure have been established for decades. This problem has been solved for longer than Python has even existed. I don't know why people keep bringing it up. Lisp isn't perfect, sure, but this is a very weak criticism.
Besides, what about mismatched delimiter problems in languages like C that have shift-reduce conflicts? Dangling else problem (http://en.wikipedia.org/wiki/Dangling_else), anyone? It seems disingenuous to complain about mismatched parens in Lisp when those are trivially handled by most decent editors, and the problem is in no way unique to Lisp.
IMO, it's really pointless to toss about theories having to do with readability for this reason or that. Programming languages are also (sub)cultural entities. It doesn't matter how many clever reasons we come up with explaining why people don't like this or that about whatever language. Most of those things are unsubstantiated, so who gives a flying expletive?
What matters is that languages attract audiences of the size they attract. It's a fact of life. It's all we know until some serious psychological/linguistic research happens. Frankly, I'm not sure if enough people care enough about what coders think for it to happen. Maybe they should. In the meantime, we should just shut up and code. (Starting with me. Why did I write so many comments today?)
To the person writing the code, yes. To the next coder maintaining the code, not likely.
http://news.ycombinator.com/item?id=2240461
On another note, why do you think lisp isn't more widely used?
When you see (def-ir1-transform ...) in the SBCL source, it's immediately obvious what is about to come along (at least if you know about compilers). Similarly old C code (eg: GCC) is completely incomprehensible in comparison.
Yes it is.
because that was the OA's point: that if a developer wanted to optimize for going uphill, choose a car rather than a bicycle. downhill? bicycle will do that with less resources, waste, cost, pollution, volume passed through, etc. pick which is better given the context and optimization goals.
This is a great quote. If you replace Python with C and Lisp with C++, it's exactly what I've been trying to articulate (as a C fan) for some time now. In fact, I think it's much more applicable to C than to Python...
It is all about getting stuff done, not insisting on using a favorite language, framework, or platform. Solving problems is what is fun and rewarding, not getting stuck on particular technologies.
Common ground: first class functions, a lot of flexibility, closures
Lisp: macros, performance?
Python: easy to learn by example, readability, batteries included
I use Python day-to-day as I don't really need a macro system that much. I also disagree that Lisp is more elegant - beauty is in the eye of the observer.
Edit: formatting.
I do not think his argument is valid. In both languages, once a program is large enough for any syntactic ambiguity to make a difference, there will be more than enough semantic clues to help solve the syntactic disambiguaties.
In other words: yes, one can write really hard to understand Lisp, where (f x) has six meanings in one line, but outside of obfuscated code contests, programmers just do not write such code.
Edit: the semantic clues I refer to are those implied by function names, names of data types, etc.
http://www.reddit.com/r/programming/comments/1ka0j/lisp_vs_p...
Every serious development group is going to need a few patterns and coding standards, because newsflash: no language is perfect. All languages need to be used properly.
If you have good standards for naming, then a simple search should tell you exactly how to read "(f x)" in less than a second. The same goes for Smalltalk method names. If an extremely important method in your application code gets named "add:" well, guess what, there are already base class methods named "add:"! This means your senders searches and implementor searches -- which are fundamental to the way lots of expert Smalltalkers code -- are going to be polluted.
Also, the same thing can apply to function names in Python!
In dynamic languages, good programmers know names are very important. Before a good, experienced dynamic language programmer names something "foo", they first search for foo! They search for things that might be confused with foo! Programmers of dynamic languages who don't do this are either ignorant or inconsiderate. This post doesn't strike me as written by someone experienced or knowledgeable with development in dynamic languages. (And if he's experienced, he's had the bad luck of having been mentored in short-sighted groups.)
There's a lot of repetition and many times it's downright verbose. But where Lisp is nice to write and hard to read, Python makes the opposite tradeoff. It's easy to read. You can determine how to interpret something—a string, a list, a function call, a definition—just by looking at the code locally.
This post takes no account of Don't Repeat Yourself and is seemingly written with no foreknowledge of this idea. Curious, since Python is powerful enough to get close to the ideal of Don't Repeat Yourself.
Strategy for creating a blog post that looks analytical and sounds good -- but only to sophomore programmers:
1) Pick a certain aspect Y of language X which can be abused
2) Give a detailed analysis for why Y is bad
3) Completely leave out any mention of known best practices for addressing Y
4) Compare to a more popular language Z. Leave out any mention of Y applied to Z.
This way, you can get some attention from sophomore programmers and provide them a way to feel better by giving them a good sounding reason to avoid studying X.
Really, this kind of sophistry is so common, there should be a name for it! (Actually, there is a way that Lisp can be easy to write and hard to read, but that has to do with macro expansion. But the tendency is for those with substantive Lisp experience to know about things like that.)
EDIT: To avoid distracting "ad hominem" nonsense. Note the argument is completely the same.
EDIT: Got read/write mixed up.
You realize that this poster is Google employee #7, responsible for "Don't be evil" (along with Paul Buchheit), and wrote the first prototype for Google Instant back in 1999?
Something else to keep in mind, from the article:
"When I read debates online, I have a bias towards the people who view these things as tradeoffs and a bias against the people who say there's only one right answer and everyone else is stupid or clueless ... When you're in a debate, consider that the other person might not be stupid, and there might be good reasons for his or her choices."
Why, are you arguing from authority on his behalf? In that case, I say he should know better! (That's a stand worth burning some karma on!)
tl;dr - If you are going to criticize language X, first take the time to learn the best practices of language X. If you don't, you're just creating more clueless noise of a kind which already exists online by the ton. Also, it seems quite lazy. All you have to do is get on IRC and ask, "Hey, I've been playing around with X. What about when this happens?" You might even learn something this way.
EDIT: Google employee #7? Makes me wonder if dozens of Google programmers have said something like, "Arrrgh, why did he have to name it that!? Did Amit write that code?"
The first half of your post was great. It addresses the arguments directly, and I completely agree. In the second half, you then go on to make assumptions about the programmer's experience and aptitude - assumptions that I've shown are quite false. If you want to debate the merits of an argument, debate the merits of an argument, don't assume that everyone who argues with you is stupid. There's a very good chance that they aren't, and then you look like a fool.
Edit: On a regular basis, I whine about Craig Silverstein or Jeff Dean or Marissa Mayer's code. "Why did s/he have to write it like that? It's cost us untold thousands of hours in coding around their nasty hacks and poor design decisions." However, I recognize the context they were operating in: they were a tiny startup of about 20 people, trying to organize the world's information. They were working basically round the clock, and they changed the world. Sometimes, building something that works for users and won't crash your datacenter is worth the price of maintainability. You have to survive before you have the luxury of hiring people that will call you stupid.
Tradeoffs. They come with every hard problem.
I challenge this. I won't pretend that Python has full macro equivalence, but decorators are damned close.
This is certainly something you can do with macros, but it's not even really close to the full semantics. Macros allow you to add new special forms to the language. If Python had macros, for example, the new `with` statements would have been a five line addition to the standard libraries.
I really don't see it as "damned close" at all.
Indeed decorators are a patch to a shortcoming in Python's ability to define anonymous functions or classes. If you could just say something like:
f = memoized(lambda x, y:
... some multiline function)
or C = coords_or_vectors(class:
... some class definition)
you wouldn't need the kludge: @memoized
def f(x, y):
...
@coords_or_vectors
class C:
...
Note that I'm not arguing for the syntax in the first two examples, just the ability to do the equivalent without special cases in the normal syntax of the language.In Scheme you wouldn't need to learn the extra syntax, learn and remember the order in which decorators apply (top to bottom?, inner to outer?).
I was actually very happy when decorators got added to Python. But let's face it, they just give you a way out of a corner Python painted itself into. They're nothing to be too proud of.
Decorators ultimately result in a function that is a substitute for another function. That function can have all sorts of wonderful runtime behavior, including access to arguments, the call stack, and other functions. Dismissing them as "adding stuff that happens every time the function is called" misses an entire dimension of their utility.
Decorators are syntactic sugar for a particular mix of assignment and function call. They're just there so you only need to write the name of the decorated object (say, function or class) once rather than three times. For example
@some_deco
def f(a, b):
return a+b
Is exactly the same as def f(a, b):
return a+b
f = some_deco(f)
Let me repeat: decorators only save you writing the name of the function two additional times. Which is nice and worth it, but not groundbreaking, and, more to the point, has nothing whatsoever to do with macros (except perhaps that in Python you couldn't do it without macros, but that's besides the point).The equivalent to decorators in Lisp is just normal higher order programming (functions taking functions as arguments, and/or returning functions), with no particular syntax.
(define f
(some-deco
(lambda (a b) (+ a b))))
It would be bad form to use a macro for something like this, which is firmly within the domain of normal function definitions and calls.(P.S.: For short functions like this, you could have said, in Python:
f = some_deco(lambda x, y: x + y)
But this breaks for multiline functions and class definitions.)
That said, the usual answers to the question of "Why do I need macros" are slowly but surely being chewed through by Python. The relatively-recent (albeit years old) addition of "with" took another previously-macro-only use case away. I'm not sure what massive win for macros is left. If your Python is "downright verbose" you're probably doing something wrong. It may not always be the Absolute Shortest (TM) but "downright verbose" shouldn't come up often.
From my point of view, not being able to define new syntax is as serious a limitation as not being able to define new functions, or data structures.
There are a number of specific examples usually cited to explain why the ability to define new syntax is important. Answering these specific examples by adding more static syntax to the language seems to be severely missing the point, to me.
Imagine it was functions instead. "Well your language already has a sort() function, but say you want to use an in-place sort instead of what you get with the standard library? You need the ability to define your own functions." "No we don't, our BDFL added an in_place_sort() routine to the standard library for the next version of the language." That doesn't make sense to me at all.
What you can do though is define behavior that varies based on the context. For example in Python you don't have function overloading or types, but with a decorator you can effectively create that type of behavior. So while you're not changing the language, you are changing the semantics of the functions you decorate.