Computer Science Canada

An Open Challenge

Author:  wtd [ Tue Feb 21, 2006 1:00 pm ]
Post subject:  An Open Challenge

This challenge is posed to all programmers that frequent compsci.ca.

Learn O'Caml.

With all due respect to Haskell, rizzix, O'Caml is in many ways a more accessible language, and yet has the ability to really open minds.

Author:  Cervantes [ Tue Feb 21, 2006 6:12 pm ]
Post subject: 

That's quite the broad challenge. I second it, although I should continue learning it, myself.

Author:  rizzix [ Tue Feb 21, 2006 11:32 pm ]
Post subject: 

Well, I'll look into it, although I should say, I'm very reluctant on learning new languages. Unless of course they are outstanding -- yes Haskell Smile

I usually just take a look at it Confused

Author:  wtd [ Wed Feb 22, 2006 12:04 am ]
Post subject: 

Haskell's a great language, but...

The way the interactive environments work is a bit more complicated from an end-user/student perspective than that of O'Caml. This is probably the biggest problem I see for teaching.

Additionally, type classes are fantastic, but running into their errors is frightening for programmers new to functional programming. It's a lot to explain just so that someone can figure out why they can't add an int and a float the way they want to.

These are good things to know, but Haskell suffers from one of the same problems that Java does from a teaching perspective: even the simplest code leverages the fanciest of tricks. Just as one needs to understand the depths of OOP to really comprehend "Hello world", one needs a deep understanding of Haskell to understand similarly simple programs.

Author:  wtd [ Wed Feb 22, 2006 12:06 am ]
Post subject: 

I think that coming from Java and its style of OOP, you'd find parameterized classes interesting. Incidentally, Scala employs the same concept.

Also, there are labelled arguments... not unlike your own little language idea. Smile

Author:  rizzix [ Wed Feb 22, 2006 12:29 am ]
Post subject: 

Yea I know. Well I was really looking at Haskell from a programmer's perspective, not from that of a teacher Smile

I can see how it can be a real pain to teach, but if you already know the stuff, well this is the language and there's nothing better -- so far.

As for labeled methods, well they are simply not the same as multi-part methods.

code:
someMethod(label1: abc, lebel2: 23);

versus.
code:
someMethod:abc part2:123;


Scala is a complicated langauge. I do give the author a lot of credit. It's not easy creating such a thing. But seriously, I'd like it cleaner. =/

IMO, if a language can define it self, it is perfect. Unfortunatly the reality is, no language is perfect by that definition. Yet Haskell is not too far off.

This does not mean non-perfect languages are not worth learning. Heck, I know Java and I like it and Java is far from perfect. The same goes for Perl, C, Obj-C etc. (Yes C, contrary to what people might think, I like C. It isin't that bad, just unsafe. Now C++, that's terrible)

Author:  bugzpodder [ Wed Feb 22, 2006 10:08 pm ]
Post subject: 

language should be only used as a tool to do the task, not as a God to worship upon

Author:  rizzix [ Thu Feb 23, 2006 12:13 pm ]
Post subject: 

Yep. And use the better langauge when there's a choice. There's usually a choice.

Author:  bugzpodder [ Thu Feb 23, 2006 3:20 pm ]
Post subject: 

But only so many languages are mainstream thus limiting our choices because of incompatibilities.

.net is a pretty good attempt at addressing cross-language issues, of course, within microsoft realm. And Java does a good job at the cross-platform implementations.

It is only natural to use .net to develop in windows, and Java to develop java bases apps. Mixing just doesn't work.

Author:  rizzix [ Thu Feb 23, 2006 8:44 pm ]
Post subject: 

Actually SUN and Microsoft are collaborating one getting these two technologies to work together.. We'll just have to wait and see how it goes.. Smile

Author:  bugzpodder [ Thu Feb 23, 2006 9:11 pm ]
Post subject: 

actually java code compiles on J#... Of course, J# only work on .net framework which primarily works on Windows platforms...

edit: Mono is an open source .net framework implementation in linux?
http://www.mono-project.com/Main_Page

Author:  wtd [ Thu Feb 23, 2006 9:17 pm ]
Post subject: 

bugzpodder wrote:
But only so many languages are mainstream thus limiting our choices because of incompatibilities.


That statemet, while true to some extent ignores one critical aspec of programming. Learning less "mainsream" languages makes you a better programmer, and that helps with the mainstream languages.

Author:  rizzix [ Thu Feb 23, 2006 9:25 pm ]
Post subject: 

little off-topic Confused : just out of curiosity (wtd), why isin't Turing a good language for learning/teaching? (with an exception to the fact that it isin't free)

Ok I understand its OO implementation is even not close to acceptable, but aside that..?

Author:  wtd [ Fri Feb 24, 2006 12:14 am ]
Post subject: 

The fact that it isn't free is a major problem. Students should feel free to experiment at any time. Turing actively discourages that.

As does the fact that it's not portable. If you want to use Tuing, you're effectively limited to using Windows, and accepting all of the problems that come with that.

There's the lack of any kind of interest in Turing outside of a very limited set of schools which poses a problem in that there are few external resources for students.

There's the overwhelming preponderance of bad instructional material, which ignores fundamental programming concepts while pushing graphics heavily.

There's the fact that it (badly) reinvents the wheel where standard input and output are concerned. This leads to some gross misconceptions about what other languages should be capable of out of the box.

As mentioned, OOP in Turing is a joke.

Author:  [Gandalf] [ Fri Feb 24, 2006 1:22 am ]
Post subject: 

"Solutions":
1. There are ways around this, and although it provides an unnecessary obstacle, it seems the larger majority of people who come here already have Turing in some way or another.

2. Ignorance. There are many other non-portable programming languages out there, so why not Turing. Razz

3. CompSci.ca

4. Ignore it, see 3.

5. No solution*.

6. No solution*.

*Other than, of course, switching to another language. Which if I am not mistaken is the whole point heh.

Author:  Cervantes [ Fri Feb 24, 2006 6:41 pm ]
Post subject: 

[Gandalf] wrote:

3. CompSci.ca

4. Ignore it, see 3.

This would be great, except very few people come to CompSci.ca. In the time that I've been here (2 years), we've grown by about 2500 users. Of all the schools in ontario that teach Turing, and all the students that pass through the CS courses at these schools, we reach a very small percentage of our target audience.

Let's face it: Holtsoft has a very strong grip on the Ontario high school cs system. If we're going to be teaching CS with Turing, it needs to be taught as well as possible. For that, Ontario needs more of its CS students coming to this site. Perhaps not as individuals, but as a class. Entire classes using CompSci.ca.

Author:  rizzix [ Sat Feb 25, 2006 1:54 pm ]
Post subject: 

Thanks for the input guys. Very helpful. Although I'd appreciate you add to wtd's list instead of providing solutions. =)

Also as far as possible don't comment on the language's syntax/semantics. Schools chose turing most likely because it closely resembles BASIC (with a slight Pascal twist), yet it isin't just as ugly.

Author:  wtd [ Sat Feb 25, 2006 2:31 pm ]
Post subject: 

Sadly it seems "new" technologies can only be accepted if they come in a familiar syntactic wrapping. Pascal was the standard, and Turing mostly resembles it, so it was a safe bet.

Since then progress in languages that both syntactically and semantically resemble Pascal has been relatively stagnant. Oberon never caught on and Ada and Eiffel are both semantic departures to a large extent.

Only overwhelming industry pressure and C curmudgeons who think they can teach anything that vaguely resembles C have pushed the likes of Java and C# into the limelight. Unfortunately if we are to judge progress by the evolution of C-syntax-ish languages, then the world must seem bleak indeed.

My conclusion: functional programming really needs a push. O'Caml is but one functional programming language among several but I believe it has the best chance at widespread acceptance.

Author:  rizzix [ Sat Feb 25, 2006 7:55 pm ]
Post subject: 

Pascal-like languages are a great teaching tool. Smile

I guess one could safely introduce functional programming langauges in later grades (gr 12?). They are not so easy to learn.

Author:  wtd [ Sat Feb 25, 2006 9:05 pm ]
Post subject: 

How are they not easy to learn, in your estimation?

Author:  rizzix [ Sat Feb 25, 2006 10:19 pm ]
Post subject: 

Well I was thinking Haskell, but as you say O'caml is easier?

Author:  wtd [ Sat Feb 25, 2006 10:23 pm ]
Post subject: 

"Easy" is a dangerous word to use. I will say that I believe it's more accessible.

Author:  rizzix [ Sat Feb 25, 2006 10:32 pm ]
Post subject: 

Not good enough, not for the schools anyway.. They want something stupid easy, so they can teach everything from the basics all the way to complex stuff without much hassle.

I'm just saying that the functional programming classes could have higher drop-out rates.. And not comp-tech department whats that to happen.

Author:  wtd [ Sat Feb 25, 2006 10:46 pm ]
Post subject: 

O'Caml can do that. I just dislike "easy" because it's way too much like "intuitive" and there's no such thing as an intuitive programming language.

Author:  Cervantes [ Sat Feb 25, 2006 11:12 pm ]
Post subject: 

It's difficult for us learnéd to say which approach is easier, for we cannot go back and test each route. Any statement we make is speculation.

I may not be able to say which is easier from the beginning, but I can say this: learning O'Caml was tough -- and I'm just talking about the functional aspect, for there's plenty I haven't done.

That said, we have to ask, "Why is it tough?" It's tough because it is a different way of looking at things. A lot of the concepts learned from other languages are not valid in a functional environment. This could explain why learning O'Caml was tough: because it goes against the grain. But what is it like for a student learning programming from the very beginning, whose grain has not been directed?

Some would argue that learning procedural programming is easier than learning functional languages because learning Turing is easier than learning O'Caml. However, Turing is designed for teaching, while O'Caml is not. It would be much more fair to compare a student with no previous programming experience learning C++ to one learning O'Caml.

Which bird will be the first to fly, now? That's a tough question, and would require a comprehensive study to answer. One point in favour of O'Caml is the interactive interpreter. Also, graphics seem to be necessary to hook a good portion of the young would-be programmers, these days. O'Caml has its own graphics library, so we don't have to go to OpenGL or SDL or others. (I haven't used O'Caml's graphics, myself, so I could be wrong about this.)

Which bird will fly better, in the end? The O'Caml bird, because O'Caml is non-standard. Consider the two new-born birds. Both develop their own approach to flying, but only one of them is widely accepted by others. The bird who learned this accepted style uses it for the rest of his life, unquestioningly. The bird who learned the other style will eventually also learn the accepted style. Now, with two styles known, this bird has a choice. This bird can raise important questions about one style, which is only possible given the knowledge of the other style. A student who learns O'Caml will, eventually, come to learn more "mainstream" languages like C++. The student who begins with C++ may be content with C++ and never learn anything else. Versatility and a wide-knowledge base is important, and beginning with a less "mainstream" language helps to ensure this is attained. Of course, Turing is also not a mainstream language, but it doesn't differ enough from the mainstream languages in ways other than just lacking things to be on the same grounds as O'Caml.

Which bird will fly faster, in the end? Certainly the O'Caml bird, for O'Caml is completely functional functionaly, but at the same time includes other programming paradigms, such as Object-Oriented Programming, and imperative programming (we have mutable variables). It is broad. After O'Caml, one is very prepared for what the rest of the programming world can throw at one; one can learn other languages, such as C++, faster.

Author:  wtd [ Sat Feb 25, 2006 11:46 pm ]
Post subject: 

I've often wondered, and I might as well ask it here...

Turing is widely accepted among those here as a good teaching language. What makes it especially well-suited to that task? What fundamental concepts does it convey particularly well?

Author:  Cervantes [ Sun Feb 26, 2006 12:12 am ]
Post subject: 

That's a difficult question to answer. Logically, I'd think Ruby or O'Caml or Io would make a better teaching language, and yet Turing is entrenched in the Ontario Computer Science classroom. With regard to Turing's ability as a teaching language, the main reason Holtsoft pushes is an easy to use syntax. But it's not the syntax that makes a language hard or easy to learn. It's something else.

My answer is the learning curve. Turing offers a very slow learning curve. I'm in my third year of high school computer science classes, and I've yet to leave Turing. My class is going to learn the fundamentals of OOP in Turing in several weeks. Turing's slow learning curve allows the less than bright students in the class to keep up.

Why does Turing have a slow learning curve? Does any other language, if taught in the classroom, have an equally slow learning curve? Maybe. However, one possible explanation for the slow curve is graphics. Since Turing makes it easy to use graphics, students make games. They get involved in the development of their game, and less involved with the development of their programming ability. Rather than advancing and learning higher programming concepts, they are content to use their current knowledge, often times struggling, to wade through a sea of graphics to produce a game. They will create a monstrosity of variables, rather than using an array, because they are more involved in the development of their game than the development of their programming ability. They will use types pseudo-objects and procedures to manipulate entire arrays of these pseudo-objects, rather than learning object-oriented programming. Perhaps it is the graphics, the detachment from programming, that makes Turing such a popular programming language.

Another possible explanation for the slow learning curve: Turing does not push you forward. In some languages, you have to learn the next concept to understand the last. For example, in Ruby, you have to understand OOP to understand "Hello World".length. You have to understand yield to understand blocks. In Java, you begin with a class, and the simplest Hello World program requires a knowledge of OOP and how Java fundamentally works to understand. These languages have some "magical" parts to them that are only explained once higher concepts have been learned. Turing does not push its users forward, for it is built from the ground up. Unlike Ruby, for example, Turing's OOP is tacked on at the end. We can go all the way from if structures and variables to types and functions and modules and collections and linked lists without ever seeing OOP. There's no incentive to learn OOP. If a student learns OOP ahead of the class, that student is exceptional. The majority of students will not.

Author:  rizzix [ Sun Feb 26, 2006 12:35 am ]
Post subject: 

Hmm, I don't know, but that's still a whole lot of speculation. O'Caml could very much be like Turing in the end. It is just an intermediary step to the real programming or computer science. The full blown attack evetually comes to you in the end, in university or college -- where you are unfortunately forced to learn C, C++ or Java.

Then again, functional programming closely relates to math. Not all students who program are comfortable with math. I don't believe you should _need_ to know math just to program (or think like a mathematician for that matter). Eventually yea you need to use math to do some useful stuff, but think of it this way: invite the student to learn math because he wants to program and not that he should know math to program.

When creating games, you eventually do need to use a whole lot of simple or complex math. Since creating games is one nice and simple way to grab the attention of most young programmers, it sort of does the trick here. (once again you don't need to enforce this, they will pick it up on their own)


Now if you are teaching a functional programming language, heck, I think the drop out rates would be pretty high. (That is a major concern with most schools).
It's just like when they make math optional in highschool, hardly anyone takes the course. =/

I think _universities and colleges_ should use functional programming languages as their preferred language for teaching programming and other computer science concepts, while high schools use an imperative Pascal-like language such as Turing.

The moment a student gets the hang of a well-defined imperative language he is intrigued and curious about the mechanics of the language. He wants to know more. This is a very healthy sign. Languages which are not properly defined make this impossible. In that regard I think Turing is a bad choice. Turing has too many exceptions to the rule. There shouldn't be any.

Author:  rizzix [ Sun Feb 26, 2006 12:42 am ]
Post subject: 

Cervantes wrote:
Why does Turing have a slow learning curve? Does any other language, if taught in the classroom, have an equally slow learning curve? Maybe. However, one possible explanation for the slow curve is graphics. Since Turing makes it easy to use graphics, students make games. They get involved in the development of their game, and less involved with the development of their programming ability. Rather than advancing and learning higher programming concepts, they are content to use their current knowledge, often times struggling, to wade through a sea of graphics to produce a game. They will create a monstrosity of variables, rather than using an array, because they are more involved in the development of their game than the development of their programming ability. They will use types pseudo-objects and procedures to manipulate entire arrays of these pseudo-objects, rather than learning object-oriented programming. Perhaps it is the graphics, the detachment from programming, that makes Turing such a popular programming language.
This depends on the teacher really. If the teacher penalizes on poor use of programming constructs whether or not the game was very creative, then the student would be forced to program the correct way. Make it clear, that although creativity is a great thing, it is not everything when it comes to programming.

Cervantes wrote:
Another possible explanation for the slow learning curve: Turing does not push you forward. In some languages, you have to learn the next concept to understand the last. For example, in Ruby, you have to understand OOP to understand "Hello World".length. You have to understand yield to understand blocks. In Java, you begin with a class, and the simplest Hello World program requires a knowledge of OOP and how Java fundamentally works to understand. These languages have some "magical" parts to them that are only explained once higher concepts have been learned. Turing does not push its users forward, for it is built from the ground up. Unlike Ruby, for example, Turing's OOP is tacked on at the end. We can go all the way from if structures and variables to types and functions and modules and collections and linked lists without ever seeing OOP. There's no incentive to learn OOP. If a student learns OOP ahead of the class, that student is exceptional. The majority of students will not.
Acutally this is a Very Bad Thing IMHO. The best language would be one where the student didin't even know there's was a whole new side to language untill he reaches a higher level, grade etc. Keeping things hidden from the student (while he's aware of it) leaves him in the dark. This should not be the case.

Author:  wtd [ Sun Feb 26, 2006 12:55 am ]
Post subject: 

Of course, I think if we're to identify a language especially well-suited to education, then we should first identify what we want from such a language, then find a language which best fits those goals.

As opposed to having a favorite language and then trying to justify its use in education. I probably won't seem flexible as I've done this analysis in detail and arrived at O'Caml. Smile

Author:  rizzix [ Sun Feb 26, 2006 12:57 am ]
Post subject: 

wtd wrote:
As opposed to having a favorite language and then trying to justify its use in education. I probably won't seem flexible as I've done this analysis in detail and arrived at O'Caml. Smile
Hmm, could you ellaborate?

Author:  wtd [ Sun Feb 26, 2006 1:31 am ]
Post subject: 

Well, I carefully considered what I thought were critical for intro programming education.

The environment has to be free, cross-platform, and preferably open source to ensure that one vendor cannot cause development to stagnate.

An interactive environment is desirable to ease experimentation with simple concepts.

It should also be possible and simple to produce stand-alone executables.

It should be easy to extend the language by interfacing to existing code written in other languages. This way functionality can be added as deemed necessary by educators.

It should expose students to (not necessarily in exact order):


  • Console and file I/O.
  • Binding values to names.
  • Variables, initialization and updating.
  • Conditionals.
  • Functions.
  • The differences between expressions and statements.
  • Looping/recursion.
  • Lists, arrays, hashes and other containers.
  • Custom data types, including record types.
  • String handling.
  • Code re-use via modules or packages. Preferably with export control.
  • OOP and everything that entails.

Author:  rizzix [ Sun Feb 26, 2006 12:07 pm ]
Post subject: 

What if Turing was improved (and made free) would you consider it over O'Caml?

Author:  Cervantes [ Sun Feb 26, 2006 2:15 pm ]
Post subject: 

rizzix wrote:
Acutally this is a Very Bad Thing IMHO. The best language would be one where the student didin't even know there's was a whole new side to language untill he reaches a higher level, grade etc. Keeping things hidden from the student (while he's aware of it) leaves him in the dark. This should not be the case.

I'm confused. You say "[k]eeping things hidden from the student... should not be the case", yet you say "[t]he best language would be one where the student didn't even know there's a whole new side to the language until he reaches a higher level...". Isn't the student not knowing there's another side to the language keeping things hidden from the student?

A language like Ruby or Java, where higher level concepts are required to understand simple programs, is good and bad for teaching. It pushes the students onwards to learn those high level concepts, but also makes the initial programs harder to understand. Turing is both bad and good, for being the opposite.

Author:  wtd [ Sun Feb 26, 2006 2:22 pm ]
Post subject: 

rizzix wrote:
What if Turing was improved (and made free) would you consider it over O'Caml?


Perhaps. I would have to ask why you'd undertake such an effort to avoid O'Caml, though. Smile

Author:  wtd [ Sun Feb 26, 2006 2:34 pm ]
Post subject: 

Oh, an important skill:

How to write robust code. O'Caml (and functional programming languages in general) offer help here.

In O'Caml aside from cases where a conditional is taking some action that has side-effects, there hase to be a catch-all else.

In pattern matching, if there is any value that cannot be matched, the compiler will complain loudly.

Surely this is important at any level of CS education.

Author:  rizzix [ Sun Feb 26, 2006 4:50 pm ]
Post subject: 

Cervantes wrote:
I'm confused. You say "[k]eeping things hidden from the student... should not be the case", yet you say "[t]he best language would be one where the student didn't even know there's a whole new side to the language until he reaches a higher level...". Isn't the student not knowing there's another side to the language keeping things hidden from the student?


Well what I'm trying to say is don't put the student into a situation where he knows there is some good reason as to why he's coding things a certain way, but he does not exactly know why he's doing it. This is what it means to leave the student in the dark and is a Very Bad Thing in my opinion.

Author:  wtd [ Sun Feb 26, 2006 7:28 pm ]
Post subject: 

Students should understand why it's beneficial to do things "the right way." They should not just chalk it up to, "oh someone way smarter than me said to do it this way."

The former is abstract thinking.

Author:  chrispminis [ Mon Feb 27, 2006 9:36 pm ]
Post subject: 

I'll perhaps try this O'Caml sometime.

But as a novice programmer, I can tell you. Turing is an incredibly easy language to learn. I don't know any other languages well enough to compare. But it is terribly easy. Yet somehow, there are many in my CS class who refuse to understand it. They remain ignorant, and it's often frustrating listening to their stupidity.

Turing does encourage the creation of games. When I chose CS as a course I aspired to create a few interesting droleries. Little did I know, with almost no knowledge of programming concepts, I was able to make a basic game. A counterpart in winnipeg, with whom I correspond, is studying another language (Can't remember, was it Turbo Pascal or something?) has not even hoped of creating a game, yet he was (is?) considerably more advanced than I, in programming skill.

Guide me, oh, programming Gods that you are. Smile

Author:  rizzix [ Mon Feb 27, 2006 9:38 pm ]
Post subject: 

Are you good in math? Smile (and by math I mean algebra)

Author:  wtd [ Tue Feb 28, 2006 2:25 pm ]
Post subject: 

http://www.cabochon.com/~stevey/blog-rants/more-ocaml.html

Author:  Cervantes [ Tue Feb 28, 2006 5:11 pm ]
Post subject: 

Quote:

And yeah, OCaml is pretty much the most expressive language I've ever used, maybe more expressive even than Ruby and Lisp, and I'm still not finished learning the more exotic features like Functors.

Whoa! Interesting. I thought of it as elegant, but not expressive, not like Ruby.

This is very cool. What a huge boost for O'Caml. People, learn it.

Author:  wtd [ Tue Feb 28, 2006 6:14 pm ]
Post subject: 

http://www.cabochon.com/~stevey/blog-rants/choosing-languages.html

Author:  chrispminis [ Tue Feb 28, 2006 8:03 pm ]
Post subject: 

Well I believe I am. Algebra is easy, and I'm capable of logic. Math has always been one of, if not, the strongest points for me. I learned at an early age and was well advanced, scoring fairly high in early math competitions. Unfortunately recently I have slacked off, and the school system has almost caught up with to the point that I actually learn stuff in math class now. I am only grade 10, so I'm not too familiar with some aspects. But then again I think its mostly the logic that helps with programming.

Author:  wtd [ Tue Feb 28, 2006 10:11 pm ]
Post subject: 

Which makes more sense to you? Smile

code:
def greet(names)
   if names.length == 0
      puts "No one?  Don't be shy!"
   elsif names.length == 1
      first_name = names[0][0]
      last_name = names[0][1]

      puts "Hello, #{first_name} #{last_name}!"
   elsif names.length == 2
      first_name1 = names[0][0]
      last_name1 = names[0][1]
      first_name2 = names[1][0]
      last_name2 = names[1][1]

      puts "Hello #{first_name1} #{last_name1} and #{first_name2} #{last_name2}!"
   else
      puts "Whoa!  Don't crowd.  There's plenty of me to go around."
   end
end


Or...

code:
let greet =
   function
   | [] -> print_endline "No one?  Don't be shy!"
   | [(first_name, last_name)] ->
      Printf.printf "Hello %s %s!\n" first_name last_name
   | [(first_name1, last_name1); (first_name2, last_name2)] ->
      Printf.printf "Hello %s %s and %s %s!\n" first_name1 last_name1
                                               first_name2 last_name2
   | _ -> print_endline "Whoa!  Don't crowd.  There's plenty of me to go around."

Author:  chrispminis [ Wed Mar 01, 2006 6:32 pm ]
Post subject: 

Aha. I recognize right away that the first one, is very much like Turing. And while I can sort of see how the second one works, the first one is much more familiar to me. So I guess it makes more sense. That's probably the point you ewre trying to make? Wanna explain the second one?

Author:  wtd [ Wed Mar 01, 2006 8:06 pm ]
Post subject: 

Well, we can simplify a few things.

code:
puts "Hello #{first_name1} #{last_name1} and #{first_name2} #{last_name2}!"


Is equivalent to:

code:
Printf.printf "Hello %s %s and %s %s!\n" first_name1 last_name1
                                         first_name2 last_name2


Thus we can simplify the two programs to:

code:
def greet(names)
   if names.length == 0
     
   elsif names.length == 1
      first_name = names[0][0]
      last_name = names[0][1]
     
   elsif names.length == 2
      first_name1 = names[0][0]
      last_name1 = names[0][1]
      first_name2 = names[1][0]
      last_name2 = names[1][1]
     
   else
     
   end
end


And:

code:
let greet =
   function
   | [] ->
   | [(first_name, last_name)] ->
   | [(first_name1, last_name1); (first_name2, last_name2)] ->
   | _ ->


Now, if the names array/list is empty, it's length will be zero. In the first of the two examples, we test for that.

If there is only one name, the length will be one. We can then use:

code:
names[0]


To access that name. Then we can further subscript that to get the individual parts of the name which we give suitable names.

If there are two names, well then we can do the same thing, basically.

If there are more than two elements, we have a catch-all clause.

Now... let's look at the O'Caml code. There are two notable things going on here.


  • I don't ever have "names" anywhere.
  • I don't have any "something = ..." variable assignments.


We have to think of functions as a way of mapping one value onto another (or an action). The "greet" function maps an empty list onto an action. The syntax allows us to express this directly, with no need to worry abount testing for the length of a particular list.

Next we say that a list with a single tuple of two names maps to a different action. The pattern mathching automatically gives names to the individualk parts of that name.

A list with two tuples of names maps to a different action, and the match anything pattern (underscore) provides a default.

Author:  chrispminis [ Wed Mar 01, 2006 11:46 pm ]
Post subject: 

Holy Crap! I actually understand what you saying. I think. I just looked back, and I understand now sort of how the second program works. In fact, I think I might understand the second one better but only because...

What do the numbers in the brackets do?

code:

first_name1 = names[0][0]
last_name1 = names[0][1]
first_name2 = names[1][0]
last_name2 = names[1][1]

Author:  wtd [ Thu Mar 02, 2006 12:42 am ]
Post subject: 

Well, let's say the array you're passing to the greet method looks like:

code:
names = [["foo", "bar"], ["wooble", "ninja"]]


That is, it's an array which contains two other arrays.

So:

code:
names[0]


Gets you:

code:
["foo", "bar"]


Therefore:

code:
names[0][0]


Gets you "foo".

Author:  chrispminis [ Thu Mar 02, 2006 8:51 pm ]
Post subject: 

Ah I see. What language was all that in anyways? And I know that there is a separate thread for this but... What programming language do you recommend I try, remembering that I AM a Turing Baby. And also, when should I switch? Are there certain concepts in Turing I should master in a familiar environment before starting a new language?

Author:  wtd [ Thu Mar 02, 2006 9:00 pm ]
Post subject: 

The languages were Ruby and O'Caml.

Learn another language now. It is possible to hold multiple programming languages in your mind at once. I would mention, though, that I do not advocate "switching". You learn multiple programming languages so you can choose between them and always have the best tool for the job.

Author:  Cervantes [ Fri Mar 03, 2006 6:19 pm ]
Post subject: 

chrispminis wrote:
Ah I see. What language was all that in anyways? And I know that there is a separate thread for this but... What programming language do you recommend I try, remembering that I AM a Turing Baby. And also, when should I switch? Are there certain concepts in Turing I should master in a familiar environment before starting a new language?


If you're going to learn Ruby, learning the basics of OOP will be a great help, as it is 100% object oriented. Tutorial One, Two, and Three available.

If you go to O'Caml, you'll definately want to be familiar with functions, though that's probably not a problem. O'Caml supports OOP, so knowing it would be helpful. However, you're not restricted to coding in an OOP manner in O'Caml, so it's not entirely necessary.

Learning a new language can't hurt. If you try and don't think you can do it, go back to Turing, learn more, write more advanced programs, and try again.


: