4 ms·
LISP has had this for years: (with-open-file (output "foo.txt" :direction :output :if-exists :supersede) (print-foo output))
by cpt1138 15y ago
LISP has had this for years:
(with-open-file (output "foo.txt" :direction :output :if-exists :supersede)
(print-foo output))
- andrewflnr 15y agoThat's not a general op/inverse thing like J apparently has, though. Also, which lisp?
- prodigal_erik 15y agoThat looks like Common Lisp, which also has a general version named unwind-protect: http://www.ai.mit.edu/projects/iiip/doc/CommonLISP/HyperSpec/Body/speope_unwind-protect.html http://www.ai.mit.edu/projects/iiip/doc/CommonLISP/HyperSpec...
- SeanLuke 15y agoNot really. unwind-protect is just Lisp's version of Java's "finally" statement.
- prodigal_erik 15y agoOh you're right, it'd need to take a param with the setup form(s) and somehow look up the corresponding cleanup form(s), sort of like getter and setter code registered with setf.
- zackzackzack 15y agoClojure has it too. If you want to abstract the pattern out even further, then all you are doing is running a function within a certain context and then getting everything back to normal. Using under in J, scoping it out with ruby, or using with-open in Lisp are all examples of the pattern, just different ways of thinking about it.
- anghyflawn 15y agoI think what you're describing is somewhat different: temporarily changing context is like a let block. So for instance in CL you can rebind standard-output to a different stream using let, but then you have to do the legwork to ensure that everything is cleaned up if something goes wrong, while with-open-file or with-output-to-stream will do part of that work.
- SeanLuke 15y agoTerminologically speaking, Scheme is a Lisp. And ELisp is a Lisp. And Autodesk VisualLisp is a Lisp. And Clojure is a Lisp. And Arc is a Lisp. But Common Lisp is Lisp.
- bowyakka 15y ago? My lisp has more lispery than your lisp ?
- Jach 15y agoIt's the difference between being Lisp and being a member of the Lisp Family (aka "being a Lisp"). It's a fun way to weed out 1-bit people from the population: http://www.xach.com/naggum/articles/3225130472400367@naggum.net.html http://www.xach.com/naggum/articles/3225130472400367@naggum.... Furthermore Common Lisp is a Lisp-2 whereas Scheme is a Lisp-1. (Aesthetically I prefer Scheme.) So really there are several working meanings at play when people say "Lisp", this is frustrating for 1-bit people. My usual heuristic in using context clues to determine which meaning is meant: if there is no qualifier for a particular family member of Lisp, I assume something is talking about the Lisp family, unless accompanied by a code example, in which case it's probably Common Lisp. When I see someone type "LISP" I usually think "This person doesn't know Lisp", just like how you'll sometimes see people type FORTH or PERL when talking about those languages. But that's not always the case so it's useful to double-check before calling them out, especially if they give code examples. (They may just be old after all. ;) )
- bowyakka 15y agoOh for sure, I was just poking a little bit of fun, thanks for the Naggum article though I had not come across that one. FWIW I tend to think of common-lisp when people say lisp in general, the schemers and clojurites tend to self identify better.
- cpt1138 15y agoSBCL