27 points · by Readmore · 18y ago · 66 comments
In an effort to not be a Blub programmer I have taken another look at Lisp to see what I'm missing, but I'm afraid that I can't really see it. I found this page http://innig.net/software/ruby/closures-in-ruby.rb which explains Closures in Ruby and after looking through that I don't really see what I could do with Lisp that I can't do with Ruby.
Again, I'm not looking for a "OMG Python is better than both" I would just like some honest answers about what the differences are. It's always worth it to learn new languages but would it be a better use of my time to start over and learn Lisp or just learn how to proficiently use Closures in Ruby?
Any comments, questions, etc. would be appreciated.
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/...
2. Macros. Macros, macros, macros. Commonplaces like "code is data" and "program-writing programs" are useful characterizations, but they make it easy to underestimate how much of a productivity booster metaprogramming can be.
Despite regular practice and a lot of first-rate reading material, I stayed clueless about macrology until I got stuck with a big ugly infrastructure problem [2] that forced me to use a bottom-up style. At some point I macroexpanded my code out of curiosity and found myself staring at pages and pages of boilerplate. Some of it -- database code in particular -- even reminded me of stuff I previously would have done by hand.
Anyway, I guess I recommend the Kool Aid...
[1] OOP owes me my Freshman and Sophomore years.
[2] I was building a platform, but I can't talk about it.
I took a look at that Closures in Ruby page you mentioned.
It says about Ruby: "This is quite a dizzing array of syntactic options, with subtle semantics differences that are not at all obvious, and riddled with minor special cases. It's like a big bear trap from programmers who expect the language to just work."
http://markwatson.com/blog/2008/01/cool-common-lisp-elephant...
Performance is a big difference:
"Common Lisp compiled code runs much faster than Ruby: typical benchmark results are about 30 times faster - but Ruby's slowness is mitigated if a lot of processing is performed in native libraries like Ferret."
I don't think Ruby has all of the functionality of CLOS or the Meta Object Protocol. Here's Matz commenting on the lack of multi-methods in Ruby:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/...
"Most OO langauge includes Ruby see methods as "element of an object" This does not hold for CLOS. You loose the tight relationship but gain a new level of flexibility."
Beyond that, I would say pick a programming methodology and there is probably a CL implementation of it somewhere out there built with macros. That's due to the greater maturity of CL and "real macros" (which have been well covered by others already). And beyond macros, the "code as data" paradigm enabled by s-expressions has other uses (I think Steve Yegge mentioned "executable log files" as an example).
For the cute version of why Lisp macros are so awesome check out chapter 8 of Practical Common Lisp (http://gigamonkeys.com/book/).
For the serious and intense version check out On Lisp (http://paulgraham.com/onlisptext.html).
They allow you to write a true DSL. Paul Graham pretty much wrote the book on the subject (On Lisp) which he graciously offers as a free download since it's out of print.
If you measured by uses of each type of operator, the role of macros would be even larger. You often write a function that you only call once, but you wouldn't do that with a macro.
Here's the last piece of code I wrote. I wrote it because I was curious to see what colors users had chosen for the top bar.
(defopa topcolors req
(minipage "Topcolors"
(tab
(each user (sort (compare > [karma _])
(keep [aand (uvar _ topcolor)
(isnt it (hexrep orange))]
(keys profs*)))
(tr (td (link user (user-url user)))
(tdcolor (hex>color (uvar user topcolor)) (hspace 30)))))))
There are 10 macros used here: defopa, minipage, tab, each, karma, aand, uvar, tr, td, tdcolor.Actually it's a hack that karma is a macro. I wanted it to be settable and was too lazy to define a setter, so I just defined it as a macro. It should be a function, and I should be able to say just (compare > karma).
I suggest you implement a terse syntax for partial-apply. For my own pet lisps, I use {}. (You are welcome to steal it ;-)
So {+ 1} would expand to (in scheme) something like (lambda rest (apply + `(1 ,@rest)). You could keep partial-applying arguments
(do
(= x {+ 1})
(= y {x 2})
then actually apply it with () (y 3 4 5) ; => 15
Your accumulator generator in Arc: (def foo (n) [++ n _])
Could be shortened to: (def foo (n) {++ n}) ; even terser than regular Arc!
The only time you'd need _ is when you want to change the order of the arguments (e.g. you'd never need {+ _ 2}, but you might need {/ _ 3}, or even {/ _ _ _ 3} if you want to jam the first three arguments in front.This has some other interesting properties:
- In a lisp-1 OR lisp-2, #'foo can be represented as {foo}.
- Your karma macro can fit in there as (compare > {karma})
- With the _ variables, it might even be a general case of your [] syntax.
- {} stands out visually. "Here be electric magic."
- If you ever implement generic functions dispatched left-to-right, you can implement partial dispatch for partial application.
(defop topcolors req
(minipage "Custom Colors"
(tab
(each c (dedup (map downcase (trues [uvar _ topcolor] (keys profs*))))
(tr (td c) (tdcolor (hex>color c) (hspace 30)))))))
Defopa means only admins can use something, but since this is defined with defop, everyone can see this page: http://news.ycombinator.com/topcolors.Edit: OK, perhaps Arc is written in Scheme, but Arc itself features CL like macros, that would explain it ;-)
This is, in my humble opinion, the biggest difference between Lisp and others.
- conditoins and restarts - CLOS - reader macros (which are distinctly different from normal macros).
If you're talking about other Lisps (besides CL), like Scheme, it's worth looking at define-syntax and syntax-rules and how they deal with some of the intricacies of creating macros with define-macro.
"RLisp is a Lisp dialect naturally embedded in Ruby"
In a similar vein, I wonder about the Erlang hype - some things are really impractical to do in Erlang, for lack of syntactic sugar and the immutable memory constraint. People were always bickering about the constraints in Java (no multiple inheritance and stuff like that), so why choose another language with impractical constraints instead?
Tree = Branch Tree Tree | ILeaf Int | SLeaf String
then a list like [ILeaf 1, SLeaf "foo"]. Another way to go takes advantage of type erasure. Currently it's just a popular extension but it lets you have lists of stuff in the same type class.
A good example is "Show" you can have a list off stuff that knows how to turn itself into strings. always a handy thing.
Basically you get the big two, group things based on what they are, or group things based on what you can do to them.
Perhaps there is a big pile of random stuff you want to hang on to, but i'd bet 99.9% of the time you really want to collect the stuff into a coherent type.
Perl6, may it rest in peace, had that feature in its spec.
I'd love to just sudo apt-get install arc in this lifetime.