Subject: RE: [hackers-il] RE: Teaching the Technion

From: Omer Musaev <[email-masked]>
Date: Tue, 11 Dec 2001 10:14:34 +0200

> -----Original Message-----
> From: mulix [mailto:[email-masked]]
> Sent: Tuesday, December 11, 2001 12:25 AM
> To: [email-masked]'
> Subject: Re: [hackers-il] RE: Teaching the Technion
> 
> 
> On Mon, 10 Dec 2001, Shlomi Fish wrote:
> 
> > Let's teach AI in Intercal! You know - it is 
> Turing-complete too. You
> > should teach a course in a language that is suitable for 
> this course. And
> > Perl is much more suitable for most courses that require computer
> > exercises. Muli can testify that he today wrote in Perl in 5
> > minutes something that would have taken him an hour to code 
> in C. And I
> > can testify for many cases where I successfully used Perl 
> as an aid in the
> > coursework we were given.
> 
> yes, yes.
> in 'introduction to operating systems' today, the lecturer showed
> how useful the unix pipe line is by demonstarating the follwoing
> command:
> 
> cat infile | tr ' ' '\n' | tr '[A-Z]' '[a-z]' | sort | uniq -c
> 
> he then asked us how long it would take to write something 
> like this in
> c. he estimated it in at least an hour of work.
> 
> so, i whipped out the trusty laptop (actually, it was already on my
> lap at this stage since i was going over some syscalltrack code) and
> wrote it. but - it's pretty obvious that to do the task 
> efficiently, you
> need a hash table and c doesn't have one. so i cheated and used
> perl, which does have hashes (maps) built in.
> 
> so i came up with this little scriptlet:
> 
> #!/usr/bin/perl -w
> 
> use strict;
> 
> my %hash;
> my $word;
> while (<>) {
>   chomp;
>   my @words = split(/ /);
>   foreach $word (@words) {
>     $hash{$word}++;
>   }
> }
> foreach $word (keys %hash) {
>   print "$hash{$word} $word\n";
> }
> 
Well, I do not know perl. But I know C++ instead:) Can we do 
solve the problem in C++ in 5 minutes? 

Of course we can!!

5 minutes and here you go:

--
#include <iostream>
#include <string>
#include <set>
#include <iterator>

using namespace std ;
int main ( int argc, char *argv[] )
{
    set< string > finals ;
    string curr ;
    while ( cin >> curr ) {
        for( int i = 0 ; i < curr.size () ; ++i )
             curr[ i ] = tolower ( curr[ i ] ) ;
        finals.insert( curr ) ;
    }
    copy( finals.begin (), finals.end (), ostream_iterator< string >(
std::cout, "\n" ) ) ; 
}
--

( compiles nicely on Sun Workshop 5 )

> total time to write? about a slide and a half, 7 minutes. so i gently
> raised my hand and told the instructor i wrote it. just as his look of
> stunned surprise was turning into disbelief, i said "of course, i
> cheated and used perl", whereupon he relaxed, smiled and said "ah, but
> anything you can do in the shell, you can do twice as fast in perl".
> 
> moral of the story?
> 

morals.end () :

Some tools are good for doing things. However, I would not suggest 
teaching generic programming with STL as introductory course.
I would not suggest teaching C++ until 3rd year - it takes some 
engineering maturity to appreciate what this tool gives you.
C++ is a tool for getting things done, not a introductory language.

So is perl.

My point is that it does not matter whatever language you use
in university - chances are that you will discover other tools.
When you do, and when you master those tools that you need 
for solving problems, you will be able to solve problems 
with much more efficiency than before.

> -- 
> mulix
> 
> http://www.pointer.co.il/~mulix/
> http://syscalltrack.sf.net/
> 
> 
> 
> ------------------------ Yahoo! Groups Sponsor 
> ---------------------~-->
> Stop Smoking Now
> Nicotrol will help
> http://us.click.yahoo.com/2vN8tD/_pSDAA/ySSFAA/saFolB/TM
> --------------------------------------------------------------
> -------~->
> 
> To unsubscribe from this group, send an email to:
> [email-masked]
> 
>  
> 
> Your use of Yahoo! Groups is subject to 
> http://docs.yahoo.com/info/terms/ 
> 
>

---

From: Shlomi Fish <[email-masked]>
Date: Tue, 11 Dec 2001 13:02:08 +0200 (IST)

On Tue, 11 Dec 2001, Omer Musaev wrote:

> C++ codelet snipped.

Not teach Perl until 3rd year? And have the poor students suffer until
then? Perl was the third language I learned, and I appreciated its
usefullness right away coming from a Basic/C/C++ (no STLs, though)
background.

It is important to teach the students how to fish. How to use tools in
which they can get the work they have to do, done quickly. Perl is such a
tool. In EE, we teach people about Matlab, because you can write Matlab
programs whose equivalent C (or C++ with every STL module in existence)
will take you much more code, work and much importantly frustration. Some
students don't use Matlab very well, and are not used to avoiding loops
whenever possible. (In Image Processing the lecturer took points off my
test because I did not use an obscure Matlab function. Which is ironic
because the Matlab exercises were not for hand-out, and nobodoy thoroughly
graded them. Furthermore, Matlab has so many functions that no one can
become familiar with all of them taking just 4 course in the subject).

Do you advise teaching Signal Processing in C? That's why I think students
should be made familiar with Perl or a similar language early on in their
studies.

> with much more efficiency than before.

I'm not sure that Perl is the ideal language for teaching everything. But
in 9 out of 10 cases it is better than C or C++. I'm not saying it is good
as an introducery programming language. But I believe it can easily be the
second language a student learns.

This reminds me that when I worked in Harmonic Lightwaves in the summer
before last, the guy who drived me there was a VB programmer. He liked it
a lot. I was positive Perl was much better, so I kept asking him if
various things that existed in Perl existed in VB too. It turned out that
it did not have object inheritance (although someone later told me it
does - I'm confused), did not have regular expressions and no lexical
scoping. He said it was possible to create complex nested data structures
in VB (using one assignment) but he did know exactly how. So, I think I'll
stick to perl for the while...

Regards,

	Shlomi Fish

> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>

----------------------------------------------------------------------
Shlomi Fish        [email-masked]
Home Page:         http://t2.technion.ac.il/~shlomif/
Home E-mail:       [email-masked]

"Let's suppose you have a table with 2^n cups..."
"Wait a second - is n a natural number?"

---

From: mulix <[email-masked]>
Date: Tue, 11 Dec 2001 15:13:31 +0200 (IST)

> solve the problem in C++ in 5 minutes?

i am ashamed to say i did not even consider c++ for the task at hand,
even though my c++ is far better than my perl, and c++ is one of my
favorite languages.

why, you ponder? i sincerely do not now. upon analyzing my mindset at
the time of the lecture, trying to figure out why i did not implement it
i c++, i came to the following conclusions:

1. shell is easily translated to perl. my mind decided to take the path
of least resistance.

2. a hash table was crucial for the task at hand. while i make sure to
make use of the stl wherever i can, i still do not "feel" it is an
integral part of c++, the same way hashes are integral to perl.

3. i've been writing too much perl lately, and not enough c++. i think i
shall ban perl for the near future and concentrate my free time
hacking on my favorite c++ project - syscalltrack's parser and
configuration utility.

omer, thank you for showing me how close i came to ruin, succumbing
to the temptation that is perl!

> 5 minutes and here you go:

doesnt do exactly what the shell pipline does, btw, but neither did the
perl script.

anyone up to writing it in other *interesting* languages? alon, didn't
you work on either intercal or sheakspeare, or both?
-- 
mulix

---

From: Shlomi Fish <[email-masked]>
Date: Tue, 11 Dec 2001 15:19:34 +0200 (IST)

> you work on either intercal or sheakspeare, or both?

I'll try to write it in Haskell. Can't promise anything, though.

Regards,

	Shlomi Fish

---

From: Shlomi Fish <[email-masked]>
Date: Tue, 11 Dec 2001 17:12:52 +0200 (IST)

Here it is. No I/O yet, because I don't remember it too well:

import FastSort

string :: String

string = "one two three oNe one tWo Three foUr one two one THREE two"

word_list :: String -> [String]
word_list "" = [""]
word_list (' ':as) = "":(word_list as)
word_list (a:as) = ((a:b):bs) where
    b:bs = word_list as

tolower :: Char -> Char
tolower c =
    if ((c >= 'A') && (c <= 'Z'))
    then (chr ((ord c) - (ord 'A') + (ord 'a')))
    else c

str_tolower :: String -> String
str_tolower string = (map tolower string)

qsort []     = []

-----------------------

It took me more than 5 minutes to write (:-(). But still it took less than
ANSI C, I believe. And definitely less than Intercal. (Muli, that was for
you)

Now all we need to do is to convert it to Lambda Calculus. I _do not_
volunteer. OmerM, do you?

Regards,

	Shlomi Fish

---

From: Shlomi Fish <[email-masked]>
Date: Tue, 11 Dec 2001 17:24:40 +0200 (IST)

Aaargh! I didn't include the whole text:

import FastSort

string :: String

tolower :: Char -> Char
tolower c =

qsort []     = []
qsort (x:xs) = qsort elts_lt_x ++ [x] ++ qsort elts_greq_x
                 where
                   elts_lt_x   = [y | y <- xs, y < x]
                   elts_greq_x = [y | y <- xs, y >= x]

count_duplicates :: [String] -> [ (String,Integer) ]
count_duplicates [] = []
count_duplicates (a:[]) = [ (a,1) ]
count_duplicates (a:as) =
    if (tail_str == a)
    then (a,tail_count+1):rest_of_tail
    else (a,1):(tail_str,tail_count):rest_of_tail where
        (tail_str,tail_count):rest_of_tail = (count_duplicates as)

total_func a = (count_duplicates (qsort (word_list (str_tolower a))))

result = (total_func string)

Two notes are in order:

1. qsort() is not very efficient for linked lists. Merge sort would have
been better, but it would have taken me more time to rip its
implementation.

2. The code could have been much more modular. But I was anxious to get it
done quickly.

Regards,

	Shlomi Fish