5 ms·
I like how code in both Python and Lisp can often look similar despite different syntaxes, different conventions, and different philosophies. Here is a toy exa
by susam 8y ago
I like how code in both Python and Lisp can often look similar despite different syntaxes, different conventions, and different philosophies.
Here is a toy example of file I/O in Python 3:
with open('foo.txt', 'w') as f:
print('hello, world', file=f)
with open('foo.txt') as f:
print(f.readline())
Here is a similar example in Common Lisp:
(with-open-file (f "foo.txt" :direction :output :if-exists :supersede)
(format f "hello, world~%"))
(with-open-file (f "foo.txt")
(format t "~a~%" (read-line f)))
This example is a bit contrived to show the similarities. Real and idiomatic code may not look so strikingly similar. But similarities in concepts, constructs, and structure of code emerge in both languages. In fact the article mentions, "Take a Lisp program, indent it properly, and delete the opening parens at the start of lines and their matching close parens, and you end up with something that looks rather like a Python program."
- lmm 8y agoAre they really any more similar to each other than to any other random language? E.g. Java: try(Writer f = new PrintWriter("foo.txt")) { f.println("hello, world"); } try(BufferedReader f = new BufferedReader(new FileReader("foo.txt"))) { println(f.readLine()); }
- sametmax 8y agoThe difference here is that you don't need to know the name of the types of all the things, nor write them. Or wrap an object in another object.
- lmm 8y ago> The difference here is that you don't need to know the name of the types of all the things, nor write them. I believe the latest version of Java would allow "var", fwiw. > Or wrap an object in another object. True. The java standard library's I/O functionality is quite low-level, perhaps excessively so. In practice there are libraries that give a more lightweight/high-level interface.
- sametmax 8y ago> I believe the latest version of Java would allow "var", fwiw. While Python gained type hinting. It's funny how in the end stuff converge.
- xyrouter 8y agoDo other popular languages support a similar construct? C? C++? Perl? The Java try-with-resources was introduced in Java 7 (2011). The Python with statement was introduced in Python 2.5 (2006). The Lisp with-open-file has been in Common Lisp Hyperspec for a long time (1960s?). Not that it takes anything away from Java. I think all languages are gradually converging to the features of Lisp.
- lmm 8y ago> Do other popular languages support a similar construct? C? C++? Perl? C? No, though I don't really think of that as an actively maintained language. C++ and Perl achieve the same thing via RAII. > Not that it takes anything away from Java. I think all languages are gradually trying to or discovering the approach the features and expressiveness of Lisp. I think language design converges over time, alternating between expressive-but-unmaintainable and structured-but-too-strict but getting closer to the ideal as we advance. In many ways Lisp isn't a complete language design at all - rather macros are there for the user to finish the language themselves.
- sedachv 8y ago> C? No, though I don't really think of that as an actively maintained language. Why would you think that? Work on the C standard is ongoing: http://www.open-std.org/jtc1/sc22/wg14/ http://www.open-std.org/jtc1/sc22/wg14/
- lmm 8y agoJust that I hadn't heard of a new release since 1999, shrug.
- kazinator 8y ago> I don't really think of that as an actively maintained language. Oh, were it only so! It's an actively diddled language.
- brianberns 8y agoC# has the "using" statement, which is effectively the same construct. F# calls it "use".
- bluefox 8y ago> "Take a Lisp program, indent it properly, and delete the opening parens at the start of lines and their matching close parens, and you end up with something that looks rather like a Python program." Sure, if you write Python in Lisp, but not if you actually take advantage of Lisp.
- sametmax 8y agoWell, technically, same for Python. I mean, you would rather write the Python snippet this way: with open('foo.txt', 'w') as f: f.write('hello world\n') Using print for that doesn't bring much on the table and a newcomer reading the code would wonder why. Plus really most IRL works just need: Path('foo.txt').write_text('hello world\n') Also you never really use readline(), since files are iterable, you generally just loop with for on it. I don't think that's the point of the author. The point is that they look like enough if you remove the cruft, that you can make helpful associations to learn the one you don't know.
- fiddlerwoaroof 8y agoI stopped using python (2.7) a couple years ago once I learned CL. But—if you’re dealing with reading standard input incrementally there are differences between readline() and the interator when it comes to buffering. Or, at least, it was easier for me to get readline to work than it was to figure out how to configure the iterator correctly. This, the whole Unicode mess and the fact that lisp allows you to write at the same level of abstraction (or greater) without sacrificing performance as much, led me to abandon Python for almost everything.
- sametmax 8y agoWhat does this have to do with the above comment ?
- fiddlerwoaroof 8y ago> Also you never really use readline(), since files are iterable
- ataturk 8y agoLISP is still superior, however. I just wish I could get a job writing LISP! I came pretty close with Clojure. I used Clojure on a (albeit throwaway) project that needed some batch processing at one job I had circa 2013. I was on my way! Then interest in Clojure died out. Groovy was #2 for me, but even that is dying out. I am doing Java 8 right now and not really enjoying it. Python would be better for me.