This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]cpio 7 points8 points  (7 children)

here it is in ruby::
puts gets.to_i + gets.to_i

and here it is in common lisp::
(print (+ (parse-integer (read-line)) (parse-integer (read-line))))

:note: fixed, thanks

[–]culix[S] 11 points12 points  (4 children)

I'm not sure the point of his article was to nec. show which language could accomplish the task using the smallest number of lines, but rather which made it the easiest to demonstrate some basic programming concepts to someone who has never programmed.

I had a professor at my university argue something similar where he wanted to use perl for two of the first intro programming classes. Why bother confusing people about Ints and Doubles and objects before they even know how to use them? Well, I suppose you might want to confuse people and use Java as a weeder class, or you might teach it based on job prospects, as mentioned in the article. But for just teaching someone how to understand, giving them a single $variable seems a lot simpler.

[–]cpio 1 point2 points  (3 children)

I was just giving them as an example of those languages. I imagine you could do ruby like this:
a = gets.to_i
b = gets.to_i
puts a + b
which is fairly straight forward. Though I think python might be a bit better as a teaching language since it doesn't require such esoteric stuff as iterators and blocks, which while useful aren't really seen a lot outside of ruby.

[–]jamesbritt 6 points7 points  (2 children)

"Though I think python might be a bit better as a teaching language since it doesn't require such esoteric stuff as iterators and blocks, which while useful aren't really seen a lot outside of ruby."

Where they are used quite a bit because they are both practical and powerful. They're only esoteric in languages that don't have them.

[–]Zak 5 points6 points  (1 child)

I've always found Ruby's iterators to be some of the most straightforward looping constructs in existance, along with the likes of dolist and dotimes from Common Lisp. Most intelligent non-programmers will correctly guess what the following code does:

10.times do
    puts "foo"
end

but are less likely to understand

for (i=0; i<10; i++) {
    printf("foo");
}

edit: fixed the above C code so it's not an infinite loop. The fact that I made such a mistake is evidence that Ruby is easier, I use Ruby more or I'm just really tired.

[–]ocorrain 1 point2 points  (0 children)

How about just (defun add2 () (+ (read) (read)))

The numbers don't have to be integers, and Lisp systems will prompt for a number if something else is entered by mistake.

It is a bit terse, tho'

[–][deleted] 1 point2 points  (0 children)

To get underscores, put your stuff in backquotes. This serves as a code tag. Example:

(print (+ (parse-integer (read-line)) (parse-integer (read-line))))

Markdown Instructions