News for hackers. Stuff that matters.
Re:Convincing Programmers They Want To Use Lisp by barrydahlberg on Monday February 21, 2011 @04:09PM (#2247193)
And now no one can forget to close the file, plus it’s shorter as well. There is no way to do this in a non Lisp language.

C# has the IDisposable + using construct for exactly this purpose. If you're not happy with that you can write your own using a lambda, just like his example...

  using (open file here)
  {
    // Do work here
  }

  WithNetFile(f => do work here);
Lisp is neat, this is not a great argument though.
Re:Convincing Programmers They Want To Use Lisp by js2 on Monday February 21, 2011 @04:44PM (#2247337)
The this the original author is referring to is macros, of which, with-net-file is merely an example.

The author makes this clear later in the post: "Once you have caught the programmer’s interest with simple macros show them some other cool things they can do, like language extensions. Experienced programmers are always looking for more control, and macros give it to them."

Re:Convincing Programmers They Want To Use Lisp by barrydahlberg on Tuesday February 22, 2011 @03:50AM (#2249246)
Yes, but he is giving this as an example of how to hook programmers into learning Lisp. I'm all on board with the macros, but you have to do better than this as an argument.
Re:Convincing Programmers They Want To Use Lisp by CJefferson on Monday February 21, 2011 @04:10PM (#2247197)
This "thing that can't be done in any other language" looks an awful lot like RAII in C++.

For example I can write:

  {
    ifstream file("filename");
    ...
  } // file closed here automatically.
or

  {
    mutex_grab g(mutex);
    .....
  } // mutex automatically released here
Re:Convincing Programmers They Want To Use Lisp by stcredzero on Monday February 21, 2011 @04:44PM (#2247338)
In Smalltalk, it's just a normal function you can add to Filename:

    doWithStreamThenClose: aBlock 
        | fileStream |
        fileStream := self newReadWriteStream.
        ^[aBlock value: fileStream]
            ensure: [fileStream close]
This allows one to do:

    (Filename named: 'mypath/foo.dat')
        doWithStreamThenClose: [ :fileStream |
             "do whatever you want with fileStream"
             "it shall be closed"
        ].
Re:Convincing Programmers They Want To Use Lisp by chc on Monday February 21, 2011 @04:18PM (#2247232)
I don't get the feeling he's used anything besides Java. His technique that's impossible in a non-Lisp language is the normal way of doing IO in Ruby, only it doesn't take a macro.
Re:Convincing Programmers They Want To Use Lisp by mark_l_watson on Monday February 21, 2011 @04:27PM (#2247268)
"adoption of Lisp as a universal language" what a bad idea! I am occasionally an enthusiastic Lisp programmer (and I wrote 2 Springer-Verlag Lisp books many years ago), but I would never advocate that any Lisp be a universal programming language.

For most programmers, they probably need at least either Java or C#, a scripting language (Ruby, Perl, or Python), and a Lisp language (Clojure, Common Lisp, or a Scheme implementation).

Re:Convincing Programmers They Want To Use Lisp by teilo on Monday February 21, 2011 @04:34PM (#2247297)
"There is no way to do this in a non Lisp language."

Wow.

Is it any wonder that you cannot convince someone to use Lisp, when you have no idea what other languages actually have to offer?

Python 2.6 has a built-in "with" construct for this. Java, C#, earlier versions of Python and (gasp!) VB have the try, catch, finally construct. Offhand, I can't think of a language that can't do this, but I'm sure they exist.

Re:Convincing Programmers They Want To Use Lisp by ScottBurson on Monday February 21, 2011 @05:00PM (#2247411)
No, you're conflating two features. He's not talking about `unwind-protect', which he hasn't even used in his example (that would be the equivalent of `try' ... `finally'). He's talking about the fact that in Lisp, you can make a single abstraction that encapulates the entire pattern of opening the file before doing something and closing it afterwards. This is not that common a language feature, though it is catching on. Python's `with' was introduced in 2.5, which came out a few months after his post was written. Scala's "automatic closures" (I would say "automatic lambdas") show another way to provide this functionality, more general than Python's `with'; I think the blogger can be forgiven for not having heard of Scala in 2006. Java has no elegant way to do this.
Re:Convincing Programmers They Want To Use Lisp by teilo on Monday February 21, 2011 @05:04PM (#2247441)
Thank you. I appreciate the correction and clarification.
Re:Convincing Programmers They Want To Use Lisp by js2 on Monday February 21, 2011 @04:46PM (#2247345)
this is in reference to macros, of which "with-net-file" is just an example.
Re:Convincing Programmers They Want To Use Lisp by teilo on Monday February 21, 2011 @04:49PM (#2247361)
No, it's not: "There is no way to do this in a non Lisp language. The closest you can come is to create an object whose constructor sends the open command and destructor sends the closed command, but even this approach has problems."

Seems pretty clear he's talking about the opening/creating and closing/destroying resources.

Re:Convincing Programmers They Want To Use Lisp by js2 on Monday February 21, 2011 @05:35PM (#2247600)
I chalk it up to poor writing. The article clearly makes no sense in that context.
Re:Convincing Programmers They Want To Use Lisp by chc on Monday February 21, 2011 @04:50PM (#2247364)
How does catching exceptions absolve us of the need to remember to close files?
Re:Convincing Programmers They Want To Use Lisp by teilo on Monday February 21, 2011 @04:57PM (#2247403)
This isn't about exceptions, though it is certainly part of the problem. Try, catch, finally blocks are commonly used to ensure that resources are released once they are no longer used. It not only forces you to remember to close the resources, it also forces you to think about what happens if something in your code breaks, and you have these open resources still in use. It's practically the entire reason that the "finally" block exists.
Re:Convincing Programmers They Want To Use Lisp by spartanfan10 on Monday February 21, 2011 @05:03PM (#2247428)
Still not convinced. I find Lisp interesting and even bought The Scheme Programming Language but can't convince myself it's more useful to me than other languages. I have been told about the power of macros before though, so to that chapter I go.
Re:Convincing Programmers They Want To Use Lisp by reedF211 on Monday February 21, 2011 @04:21PM (#2247245)
This article is nothing other than language fanboyism and I am not sure what it's doing on the front page of HN.
Re:Convincing Programmers They Want To Use Lisp by stcredzero on Monday February 21, 2011 @04:33PM (#2247290)
I've half a mind to start a wiki for programming language fanboy/troller tropes. That way, we can just link to that.