Re:Peter Norvig's Sudoku solver: Clojure & Python side by side
by nickik on Wednesday March 30, 2011 @05:31PM
(#2389337)
len(values[s]) for s in squares)
(map (comp count values) squares)
How is that better! If you know the standard function this is super easy (in both languages). Note clojure is just one pure function call nothing fancy. (inc (apply max
1+max
The expressivness is tiny bit better in python because the max function looks at the type of the input. apply is a commen pattern that is easy to anderstand. The python way is a little bit easier to read but the clojure way makes the library simpler. line = '+'.join(['-'*(width*3)]*3)
line (join \+ (repeat 3 (join (repeat (* 3 width) \-))))
The thing that makes python shorter here is that they alow math on non number. I don't think that a good idea in general but that up to the language designers taste. The diffrence between the code is basiclly that in clojure you have to use repeat two times instead of * and one more join.So only real diffrences are on the first line clojure uses apply and on the second repeat instead of join.
I think is just a matter of training. Much harder then the language reading is to know what the standard functions really do.
Re:Peter Norvig's Sudoku solver: Clojure & Python side by side
by dfan on Thursday March 31, 2011 @09:09AM
(#2391368)
I love Lisps, and I have the same problem. You really have to learn to approach reading code in a different way. Lisp (and Haskell, say) are so information-dense that you just have to look closely at every word to understand what is going on, as opposed to C or Python where you can glance at a "paragraph" of code and get a decent sense of what it is doing , because it is basically executing a set of statements in order. Probably the closest you get to this dense unpacking issue in Python is when reading a gnarly list comprehension.
The upside is that the code actually is smaller, so you have to read less of it. But I agree that it is fatiguing to read dense code that you can't really skim.
Re:Peter Norvig's Sudoku solver: Clojure & Python side by side
by swannodette on Wednesday March 30, 2011 @01:30PM
(#2388238)
(inc (->> squares (map (comp count values))
(reduce max)))
Similar to the Python | operator that came up yesterday.[deleted]
Re:Peter Norvig's Sudoku solver: Clojure & Python side by side
by evanrmurphy on Wednesday March 30, 2011 @01:28PM
(#2388228)
Hmm... I just tried it, and backslashes don't seem to work as a way to escape Hacker News markup. Were you kidding?
Update: Ah, it looks like you can get literal asterisks if you use your whitespace carefully:
* asterisk * another one emphasis
let width .. inc apply max of map comp coun, wtf. Let's start from the end. map all squares with comp count values, take the max of those, increment it, put that in width. ok
then, line join repeat 3 join.. wtf. let's try it from the end; minus three times the width repeat.. ok, repeat 3width '-', then, join this, repeat it 3 times, then join again with +, and put that in line.
I always struggle like that when reading lisp, people always give example like:
(map square '(1 2 3 4)) where you can read it all in one pass.. but real code is usually more complicated. So, if I had to rewrite that more clearly, I'd probably just separate it in more functions so I don't have to have more than, say 4 nested ().
And, yeah, the problem is not that much the "read from the end", but more about the actual work of each line of lisp being a puzzle to guess where to start reading it. Some are better to read up front, others are better to start from the last. Sometime you skip the first part, and read the last part, then come back at the beginning. That is what trouble me. I want to be able to read code as fast as possible - skim over it while understanding it.
Now, you might argue that you can write nested stuff in every languages. True enough BUT it seems like the philosophy in python is more about making things clearer and using more function if it makes everything simpler/easier to maintain and understand.. while in practically all lisps code I read, it feels like it is a write-only code without thought about you will read it after.
Is it because all the "Here's why lisp is better" are written by new-lisper who are too much excited as they can now use closures and nest everything rather than using simple, yet useful, normal functions?
When I read lisps forum, it's always about how that or that doesn't support tail recursive function or why macro rocks everything, but never talking about readability and good practices about not nesting everything up. Yeah, they say to use real editor, to never look at the (), to align thing smartly.. I don't know for you, but I feel like my brain works in a kind of stack. I can put some stuff in my short memory, but I can't put more than say 5-6 stuff at once. And when I see this:
It just seem too much to read it quickly; I need to stop and think. Yeah, some laugh at me saying "Yeah, you need to think*! You are not used to that! Well, no, when I'm reading a book, I like to think about the concept or the story, not trying to figure out how to read that complicated sentence.When I read python code, I think about pattern, modularity, what's the goal of the actual file. When I'm reading lisp, I can't go quickly into it because of those 10 levels nested function.
/end of rant.