?

Log in

No account? Create an account
pig latin: perl vs. C++ - brad's life — LiveJournal [entries|archive|friends|userinfo]
Brad Fitzpatrick

[ website | bradfitz.com ]
[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

pig latin: perl vs. C++ [May. 27th, 2004|09:43 pm]
Brad Fitzpatrick
[Tags|, ]

I went to help out at my littlest brother's high-school programming class today. Freshman intro to programming... they learn C++. Painful, but I can't think what the best language to learn on would be. I suppose simplified C++ isn't so bad... mix of high-level and seeing how the machine actually works, kinda.

Anyway, their project was to write an English to Pig Latin converter. I explained how to setup one function w/ a little state machine to tokenize the sentence into words, one function to pigify the word once boundaries are found (including "qu" support, -ay vs -way suffixes), etc. Also showed them how to use the debugger. (Visual Studio)

So after their 3 pages of code (small monitors) I showed them how it's just one line in Perl and walked them through each character of the Perl version briefly. *evil grin*
LinkReply

Comments:
[User Picture]From: cjcollier
2004-05-27 09:54 pm (UTC)
I'd go for C# as a good intro language, assuming they've got VS.NET 2003. The environment just makes it easy.

But I'm speaking from experience. In this case, I'd prefer to be speaking from ignorance. *shrug*
(Reply) (Thread)
[User Picture]From: brad
2004-05-27 10:15 pm (UTC)
They have ancient computers and their version of Windows is already pushing the minimum requirements, then some.

I'm thinking Linux and something clean like python (which admittedly I don't use) with some graphics library to make it all seem more real/tangible.
(Reply) (Parent) (Thread)
(Deleted comment)
[User Picture]From: jwz
2004-05-27 10:34 pm (UTC)
The best language to learn on is Scheme, because that's what Structure and Interpretation of Computer Programs is written in. Second best would be Java, I guess.

Let it be proclaimed throughout the land. Good night.
(Reply) (Thread)
[User Picture]From: toast0
2004-05-27 10:40 pm (UTC)
I think Java's everything is an object, and main is a class method puts way too much syntax in the way of the first several programs people write when learning programming.

I also think I should endeavour not to participate in holy wars (even if I start them), so this will be my only comment in this thread.

(Reply) (Parent) (Thread)
[User Picture]From: jwz
2004-05-28 12:13 pm (UTC)
Well, that's why I think lisp-like languages are best for an intro: there's almost no syntax. There are like, three rules total. You spend your time learning about functions and abstraction and programming instead of first having to spend forever memorizing where the semicolons and commas and colons and periods and arrows go.

But I think Java reaches some kind of local maximum in the space of "not a terrible language" versus "actually applicable in the real world." Meaning, it has a decent object system, and yet it's still C-like.
(Reply) (Parent) (Thread)
[User Picture]From: colin
2004-05-27 10:43 pm (UTC)
λ.</a>
(Reply) (Parent) (Thread)
[User Picture]From: colin
2004-05-27 10:40 pm (UTC)
Picking an intro language is tough. The main goal in an intro programming course is to teach students how to approach a programming task, not how to program in a specific language. Along the line, they should be exposed to various important data structures and programming paradigms. It's tough to pick a language that balances all the necessary elements. Which brings up another important thing to teach: Choose the right language for the job.

I think Python is a good choice, but I feel that it's beneficial to expose students to two completely different languages in the intro. That way students learn that choosing the right language for the job is also part of being a good programmer.

Another important thing for students to understand are tradeoffs. Did you mention that the Perl interpreter is written in C? There's a lot of overhead there. If the pig latin converter needed to be really fast, Perl wouldn't be a good choice, but if it needed to be written very quickly and efficiently so it could be modified later, Perl would be the better choice.

I guess what I'm saying is there's more to teaching programming than teaching a language, so there are good intro languages, but none of them is best.
(Reply) (Thread)
[User Picture]From: gen_witt
2004-05-27 10:49 pm (UTC)
I really think that Java should be your fist taught OO, language. I dislike having to say it but I think it's true, you can get the basics of what objects are for, polymorphism (and dynamic binding), inheritance, overloading, RTTI, "modern" exception handling, containers (which while not part of Java persay, are an important thing to get to relativly quickly), and some multi-inheritance with interfaces. All this without the shackels of not having everything inherit from a single class (and thus templates), operator overloading, C backwards compatibility (ok C++ isn't fully backward compatible but still), true multiple inheritance, and really all the quirks of C++.

That said, I'd start with a non-OO procedural language, almost certainly C (mainly because it shares Java's syntax), or possibly Pascal (although I'd be the laughing stock of the whole town).

After java, comes C++, with Effective C++, and Design and Evolution, required reading. Assuming at this point you've taught all the basics of data structures and algorithms, your free to move into functional languages, logic languages, and programing language theory, and on to compilers. Or to networking, or operating systems, and systems programming, it's really an open ended descision then.

I'd hold off on Perl because it's really designed to do things quick and dirty for people who know how to program, it's really focused on text processing, there's not much professional demand for it, and it's an out growth of the bourne shell, and most students will have yet to touch on *IX until they're early programming classes. Personally for my text processing I prefer AWK to PERL (which is much less full featured, I know). Graphics are great for keeping people interested, but hard to teach along side of fundamentals, and also a royal pain to grade and thus tend to be less well checked than console applications, which can be checked by script, for all sorts of corner cases.

And whatever a teacher decides, they should not teach SWING (or really any GUI toolkit) along side the introductory programming course. "Just ignore these 4 pages of OO goodness and focus on your for loop," does not make students happy. And yes, text processing in C is no fun.
(Reply) (Thread)
From: ex_kalyan
2004-05-27 11:04 pm (UTC)

Pascal

I started out with pascal in school and honestly thats what got me hooked on to programming.
(Reply) (Thread)
[User Picture]From: iamo
2004-05-27 11:16 pm (UTC)
Surely not three pages ;).

I'd be curious to see your code for both.
(Reply) (Thread)
[User Picture]From: brad
2004-05-27 11:43 pm (UTC)
I don't know the C++ standard library, and they didn't even know functions, variable scoping, or C strings.

It was slow going.

Sorry, didn't save a copy of the code.
(Reply) (Parent) (Thread)
[User Picture]From: iamo
2004-05-28 12:00 am (UTC)
Yeah, didn't mean to diminish what you did, I was sort of just saying that it's kind of an unfair comparison. But I think gen_witt made my point better than I did wrt the reason why it's possible to do it so small in perl. If you had done a full manual state machine in perl, it probably would have been about the same length (minus some of the wordy cruft of C++).

But I'm a big fan of C++ and you're a big fan of perl, and there's nothing wrong with that :)
(Reply) (Parent) (Thread)
[User Picture]From: brad
2004-05-28 12:23 am (UTC)
Right tool, right job.

When I say 3 pages, remember 15" monitor, w/ visual studio menus/toolbar/debug window.....

(Reply) (Parent) (Thread)
[User Picture]From: mart
2004-05-28 05:14 am (UTC)

As the user of a 15" monitor myself (must graduate and get money!) I can vouch for how tiny that little window is. It's probably one of the reasons I don't use Visual Studio! ;) (a text editor's good enough, but then I guess I'm not learning)

What concerns me more is that programming wasn't even acknowledged as existing when I was at high school. I guess this shows a difference in the education systems more than anything, though.

(Reply) (Parent) (Thread)
[User Picture]From: scosol
2004-05-27 11:19 pm (UTC)
did you give them all JAPH buttons? :P

huh- best first programming language?
i would say java-
(Reply) (Thread)
[User Picture]From: mart
2004-05-28 05:19 am (UTC)

I think an important part of a first programming language is being able to show the Hello World example without explaining a tonne of other things at the same time:

public class App {
    public static void main(String[] args) {
        System.out.println("Hello, World!");
    }
}

I seem to remember that coming up in my first programming class, and the lecturer having to essentially say “ignore all this crap” about the class, the meaning of public static, what System.out is, etc. Having an initial example that you can't actually explain fully (within a short space of time) seems like a bad place to start to me.

Having said all that, my university started with Java, and didn't require any prior programming experience. (Made for a boring first year) I started on Commodore 64 BASIC, but fortunately I managed to escape the BASIC curse long ago.

(Reply) (Parent) (Thread)
[User Picture]From: scosol
2004-05-28 09:45 am (UTC)
Huh- well that's kinda weak of your professor IMO- all that was required was a "Java has a large set of build in system classes, the one we're using here is in the System->output section- and we're using the "print line" method to print the text "Hello World""

And well- "first programming language to learn" is a little different than "intro to programming" no?
In the latter, I would think that the actual language choice is fairly irrelevant- it being more important to convey the basic concepts of control structures, objects etc.
(Reply) (Parent) (Thread)
From: sheehan
2004-05-27 11:45 pm (UTC)

state machine?

you can get really close with the following:


#include
#include
#include
#include
#include

using namespace std;

//omitted functor

int main()
{
vector strings;
copy(istream_iterator(cin), istream_iterator(), back_inserter(strings));

PigLatinize pl; //functor
for_each(strings.begin(), strings.end(), pl);

return 0;
}


this doesn't handle punctuation that well, but the common cases (ie, leading & trailing punctuation) are really easy to cover...

unless of course you wanted to jump right into state machines :)

--scott
(Reply) (Thread)
From: evan
2004-05-27 11:53 pm (UTC)

Re: state machine?

the things that c++ calls "functors" are entirely different from what the rest of the programming languages and math world calls functors.

someone once mentioned they were using functors in a project of theirs and it took me quite a while to realize they were just talking about first-class functions.
(Reply) (Parent) (Thread)
[User Picture]From: brad
2004-05-28 12:21 am (UTC)

Re: state machine?

Weird.
(Reply) (Parent) (Thread)
[User Picture]From: gen_witt
2004-05-28 12:28 am (UTC)

Re: state machine?

Java uses this particular definition of functor also, and these "functors" arn't even close to as general as first-class functions.

Although I never really got what the category theory functors, had to do with the functional programming construct, care to explain?
(Reply) (Parent) (Thread)
From: evan
2004-05-28 01:33 am (UTC)

Re: state machine?

What's even weirder is that C++ templates kinda actually do correspond to functors (at least in their use)...


(disclaimer: i've only been studying category theory for about a week.)

As I understand it, there are many different levels at which functors come up.

1) In programming languages you have all sorts of values floating around: 1, "hi", etc. Then you have this thing we call the "type" of a value: int, string. You can then say a given type roughly corresponds to a category: for example, in the category "int" you can imagine morphisms like "successor" that map from an int object to another int object. [Here, "object" is the category-theory term for "things within the category"; don't confuse it with a Java object.]

In category theory, a functor is a map from a category to another category that holds some particular properties (that I don't understand well enough to do justice to in an attempt to explain). So the programming language analog is like a function, but instead of being from ints to ints, it's from types to types.

For example, in C++ (functors aren't limited to functional languages!) I suppose you could call "vector" a functor: I can't create a value of type "vector", I can only apply it to a type and have it produce a type. That is, I have a type like "int", and I have a type like "vector", and I can create values of both types. These are more commonly called "type constructors".

2) O'Caml also calls its functions from structures to structures functors. I can't explain structures well enough to make much sense of this-- they roughly correspond to namespaces but you can plug them in for each other. Maybe: closer to interfaces, and a functor is a parameterized interface.

---

Just as values have types, types themselves have kinds. Most types, like most values, are usable in isolation (you can use "1" by itself just like you can use "int" by itself), but others must be applied to be used (you apply "successor" to 1, you apply "list" to "int").

You'll note that the syntax for "calling" a template in C++ is different than the function call syntax. Strangely, in O'Caml -- which uses the sorta traditional "put them next to each other" syntax for calling functions: "successor 3" would evaluate to 4 -- has the output of its type constructors written in reverse (applying the "list" type constructor to "int" is written as "int list"). Additionally, O'Caml functors use a different syntax: A(B), like a function call in C.

Haskell (which is strongly influenced by purity and mathematics) uses the same syntax for all of them. This unification is quite cool: their type constructors quite literally are written as functions from types to types.

---

Category theory is so broad it applies in a bunch of different situations. (This apparently is a common criticism: too broad to be useful.) But discoveries in category theory actually have informed programming languages: Haskell monads were discovered/created after they were discovered in the mathematics world.

See also: learning haskell notes on Haskell functors: "... every type constructor is a functor but not vice versa; a functor is a type constructor PLUS a function fmap satisfying some laws."
(Reply) (Parent) (Thread)
From: sheehan
2004-05-28 12:42 am (UTC)

Re: state machine?

a googling tells me that this c++ perversion of the word has spread to java, next the world!

hopefully, eventually c++ will be as powerful as lisp (just with a different syntax!) ;)

--scott
(Reply) (Parent) (Thread)
[User Picture]From: ydna
2004-05-28 01:25 am (UTC)
Assembler. If'n it was good 'nuff for me, it's good 'nuff fer you awl.
(Reply) (Thread)