Hugh Aguilar wrote:
> On Apr 23, 1:27*pm, "WJ" <w_a_x_...@yahoo.com> wrote:
> > Hugh Aguilar wrote:
> > > On Apr 21, 10:03*pm, "WJ" <w_a_x_...@yahoo.com> wrote:
> > > > Ruby:
> >
> > > > _, numwords, numpatterns = gets().split.map{|s| s.to_i}
> >
> > > > words = (1 .. numwords).map{ gets().strip }
> > > > patterns = (1 .. numpatterns).map{ gets().strip }
> >
> > > > patterns.each_with_index{|pat,i|
> > > > * regex = Regexp.new( pat.gsub("(", "[").gsub(")", "]") )
> > > > * printf "Case #%d: %d\n", i, words.grep(...
> >
> > > I don't know anything about Ruby, but I can certainly admire the
> > > conciseness of your program. How does it compare in speed to mine?
> >
> > It takes 6.5 seconds for the large input file on my laptop.
> >
> > The program is run this way:
> >
> > ruby code-jam.rb Code-jam.in
> >
> > I'll explain a few things.
> >
> > _, numwords, numpatterns = gets().split.map{|s| s.to_i}
> >
> > gets() reads a line from stdin or the file given on the
> > command-line; .split splits that string on whitespace,
> > yielding an array or list of strings;
> > .map works as in Lisp, in this case converting each string
> > in the array or list into an integer.
> >
> > words = (1 .. numwords).map{ gets().strip }
> >
> > (1 .. numwords) is a range; some examples of ranges
> > (the .to_a expands the range into an array or list):
> > * (1 .. 5).to_a
> > * * * ==>[1, 2, 3, 4, 5]
> > * ("b" .. "f").to_a
> > * * * ==>["b", "c", "d", "e", "f"]
> > Here I'm simply using the range to read numwords lines
> > from the file. *I could have done something like
> > * words = []
> > * numwords.times{ words << gets().strip }
> > .strip removes all whitespace from beginning and end
> > of the string.
> >
> > patterns.each_with_index{|pat,i|
> >
> > Iterating through the patterns; pat, of course, is
> > the pattern. *i is assigned the index of the current
> > item, starting with 0 (which means my output has an
> > off-by-1 error).
> >
> > * printf "Case #%d: %d\n", i, words.grep( regex ).size
> >
> > .grep works on an array or list of strings, selecting
> > only the items that match the regular expression.
>
> Ruby is pretty impressive; I should learn more about it. BTW, do you
> know Icon?
Years ago, Icon was my favorite language. (I have the book by Griswold
& Griswold.) Then I switched to Ruby.
>
> As I've said before, my Straight Forth will only be for micro-
> controllers. I will have a "sister language" that will be used for
> desktop-computer programs that are related in some way to the micro-
> controller programs. I had been planning on Racket, but maybe I should
> consider Ruby instead.
I'm trying to learn some Racket, too.
Ruby lacks 2 things (compared to Racket):
1. High-speed looping. (Don't try to generate the Mandelbrot Set
with it.) Most of the other "scripting" languages are faster
than Ruby, I believe.
2. Macros.
> Ruby is a lot more popular,
I wonder which language would have more users if one didn't count
those who just use Ruby to power web sites (Ruby on Rails, etc.).
> although I think
> that Racket has more novice-oriented documentation (a good thing, as
> most micro-controller enthusiasts are more interested in hardware than
> in software; they don't want to learn something complicated even if it
> is more powerful, especially for desktop-computer programming which is
> only peripherally related to micro-controllers).
When people like that learn Ruby, they should skip the advanced
object-oriented features, as I did. I basically use Ruby as one
would use Perl, Awk, Lua, Python, or Scheme. I very seldom create
a new class; I just write some functions. Ruby on Rails is Greek
to me.
>
> Over on comp.lang.lisp, I've heard Ruby described as "Matz-Lisp" ---
> meaning that Ruby is just Lisp with a more friendly syntax (for people
> accustomed to infix). Would you consider that to be an accurate
> description of Ruby?
Perhaps. The creator of Ruby wrote this:
* Ruby is a language designed in the following steps:
* take a simple lisp language (like one prior to CL).
* remove macros, s-expression.
* add simple object system (much simpler than CLOS).
* add blocks, inspired by higher order functions.
* add methods found in Smalltalk.
* add functionality found in Perl (in OO way).
So, Ruby was a Lisp originally, in theory.
Let's call it MatzLisp from now on. ;-)
matz.
Paul Graham, the Lisp guru, has advised those who cannot
use Lisp at work to see if they can use Ruby, which he
considers somewhat Lisp-like.
No matter how many other languages I dabble in, I'll probably
keep using Ruby for many things. It often makes programming
so easy that it's almost boring.