According to this article by Ned Batcheleder python does destructuring bind by default.
Well, I did not know that.
According to this article by Ned Batcheleder python does destructuring bind by default.
Well, I did not know that.
After playing around with Lisp for a few days I’ve diverted myself back to a small Java project. Almost immediately I’m missing the Lisp environment! To be sure there are some things that I prefer about my Java environment, which is Eclipse, but I just miss having an interactive top-level so much! It’s just amazingly convenient to be able to try sunning a fragment of code, and the ability to tweak it and try again if you hit any problems is such a productvity boost.
I’ve been playing a little with learning Lisp (not that I’ve much free time, but hey, what the hell). This example, taken from Dave Lamkins’ Successful Lisp, more than any other that I’ve seen so far, expresses the power of the language.
(defun open-bracket (stream char)
`',(read-delimited-list #] stream t))
(set-macro-character #[#'open-bracket)
(set-macro-character #] (get-macro-character #)))
Here’s what’s happening: we’re redefining the syntax of the language dynamically! Normally, if I type [1 2 3] into a Lisp interpreter it will raise an error, something along the lines of variable [1 has no value, but after running that little code snippet I can now type [1 2 3] and it will create a three element list populated with the numbers 1, 2, and 3. Here’s the equivalent in a Java like syntax.
class OpenBracket implements JavaLanguageParser {
static List list;
public void nextToken(RuntimeEnv env, StreamTokenizer stok) {
if (list == null)
list = new ArrayList();
int tok = stok.nextToken();
if (tok == TT_WORD)
list.add(stok.add(stok.sval));
else if (tok == TT_NUMBER)
list.add(new Double(stok.nval));
}
}
class CloseBracket extends JavaLanguageParser {
public void nextToken(RuntimeEnv env, StreamTokenizer stok) {
if (OpenBracket.list ==null)
throw new ParseException("mismatched brackets");
env.pushObjectOntoStack(OpenBracket.list);
OpenBracket.list.clear();
OpenBracket.list == null;
}
}
Lang.defineNewKeyword("[", new OpenBracket());
Lang.defineNewKeyworkd("]", new CloseBracket());
Then writing List foo = [1 2 3 bar] would yield a new ArrayList populated with 3 Double objects and a string. Even with my friendly psuedo-code it’s a lot more work, but to actually do this is impossible.
I assume that this is what folks like Paul Graham mean when they say things like ‘If you re writing a text-editor, you can turn Lisp into a language for writing text-editors. If you re writing a CAD program, you can turn Lisp into a language for writing CAD programs’ and it strikes me as an amazingly powerful concept.
I think I really like this language.
There’s a fascinating article by Paul Graham about how computer languages evolve, and what a language would look like one hundred years from now. Interesting stuff.