Computer Science Canada Translations from Turing |
Author: | wtd [ Thu Oct 28, 2004 7:00 pm ] | ||||||||||||||||||||||||||||
Post subject: | Translations from Turing | ||||||||||||||||||||||||||||
I did this in the General Discussion forum a while back, translating C++ and Java code into equivalent Eiffel and O'Caml code. I know at least one person found it useful, so I thought I'd provide an even more useful service: translating Turing (which I'm guessing a fair number of people here know to at least some extent) code into other programming languages. I'll try to keep the examples in the same order, but I don't make any guarantees. We start with "Hello, world!", which aptly demonstrates basic output and commenting syntax. Turing
C
C++
Java
C#
D
Perl
Python
Ruby
O'Caml
Eiffel
Pascal
Ada95
Javascript
|
Author: | wtd [ Thu Oct 28, 2004 7:25 pm ] | ||||||||||||||||||||||||||||
Post subject: | |||||||||||||||||||||||||||||
Now, abstracting the "Hello, world!" into a procedure called "say_hello_world". Note: That name may appear slightly different depending on the naming conventions of a language. Where possible, forward declarations are used. Turing
C
C++
Java
C#
D
Perl
Python
Ruby
O'Caml
Eiffel
Pascal
Ada
Javascript
|
Author: | Andy [ Thu Oct 28, 2004 7:33 pm ] |
Post subject: | |
woa there is a D? |
Author: | wtd [ Thu Oct 28, 2004 7:41 pm ] |
Post subject: | |
dodge_tomahawk wrote: woa there is a D?
Yes. It was created by C and C++ compiler vendor Digital Mars, to address what they felt were shortcomings in C++. |
Author: | wtd [ Thu Oct 28, 2004 10:20 pm ] | ||||||||||||||||||||||||||
Post subject: | |||||||||||||||||||||||||||
Of course, we're still just saying "Hello, world!". There's no flexibility. Instead, let's create a procedure "say_hello" which takes one string argument "name" indicating who the procedure should greet. For this example, we'll greet "Bob". This one will demonstrate specifying a procedure with an argument and then calling it that way. Turing
C
C++
Java
C#
D
Perl
Python
Ruby
O'Caml
Eiffel
Pascal (This one should work, but FreePascal is giving me a syntax error)
Ada - Strings in Ada are tricky enough that I've left this one out Javascript
|
Author: | wtd [ Thu Oct 28, 2004 11:36 pm ] | ||||||||||||||||||||||||||
Post subject: | |||||||||||||||||||||||||||
There's still a problem with the previous examples. They're hardcoded to send the greeting to standard output. This isn't very flexible. A better, more flexible approach would be to have a function called "greeting" which accepts a name and returns a string that can then be printed to any media. For simplicity we'll just print it to standard output anyway. Turing
C
C++
Java
C#
D
Perl
Python
Ruby
O'Caml
Eiffel
Pascal
Javascript
|
Author: | wtd [ Fri Oct 29, 2004 12:46 am ] | ||||||||||||||||||||||||
Post subject: | |||||||||||||||||||||||||
To expand on this, let's get user input instead of just hardcoding in "Bob". Additionally, the greeting function should test the input name. If the name is "Clarence", the reply should be "Ha ha! Clarence...". If the name is "Sid", the response should be "Sid? What a maroon." Otherwise, the greeting should be the standard greeting we've been using. Turing
C
C++
Java
C#
D
Perl
Python
Ruby
O'Caml
Eiffel
Pascal
|
Author: | Delos [ Fri Oct 29, 2004 8:32 am ] |
Post subject: | |
As per usual wtd, quite awsome. BTW, is there any particular reason why you know the basics in so many languages? Pastime? |
Author: | wtd [ Fri Oct 29, 2004 1:33 pm ] |
Post subject: | |
Delos wrote: As per usual wtd, quite awsome. BTW, is there any particular reason why you know the basics in so many languages? Pastime?
Essentially. Each language you learn makes it easy to learn others, so I figure learning a whole lot of 'em... |
Author: | rizzix [ Fri Oct 29, 2004 3:36 pm ] | ||||
Post subject: | |||||
ok i dont understand why u took the longer way out for Java and c# in the example obove the last one u've written.. wtd wrote: public static String greeting(String name) { StringBuffer sb = new StringBuffer("Hello, "); sb.append(name); sb.append("!"); return sb.toString(); } well you could've use the concatenation operator ('+' in the case of java and C#) just as you did for D. the concatenation operations are quite optimized by the complers in most cases (definately for java) cuz its soo commonly used. so just an implementation of the same function with the '+' opeator in java would be:
and u can do something similar for c#
|
Author: | wtd [ Fri Oct 29, 2004 5:34 pm ] |
Post subject: | |
I probably should have used the concatenation operator. I'll edit it to simplify those examples. |
Author: | wtd [ Fri Oct 29, 2004 9:27 pm ] | ||||||||||||||||||||||
Post subject: | |||||||||||||||||||||||
Let's extend the previous example to greet someone with a fitrst and last name. Turing
C
C++
Java
C#
D
Perl
Python
Ruby
O'Caml
Eiffel
|
Author: | zomg [ Wed Nov 17, 2004 1:31 pm ] |
Post subject: | |
i never realized there was this many programmin languages i only heard of c, c++, java, vb, python, turing |
Author: | wtd [ Wed Nov 17, 2004 7:03 pm ] |
Post subject: | |
shadow master wrote: i never realized there was this many programmin languages
i only heard of c, c++, java, vb, python, turing 99 bottles of beer on the wall in 621 languages. |
Author: | Andy [ Thu Nov 18, 2004 10:27 am ] |
Post subject: | |
shiz... thats a lotta code |
Author: | wtd [ Thu Nov 18, 2004 6:53 pm ] |
Post subject: | |
Amazingly enough, that list is probably missing a lot of languages. |
Author: | DtY [ Wed May 20, 2009 7:38 pm ] | ||
Post subject: | RE:Translations from Turing | ||
Wouldn't the line with malloc(); lead to a memory leak? You're not freeing it. [edit] Sorry, for bumping, I didn't realize this had been dead for years |
Author: | wtd [ Fri May 22, 2009 12:06 am ] | ||
Post subject: | Re: RE:Translations from Turing | ||
DtY @ Thu May 21, 2009 8:38 am wrote:
Wouldn't the line with malloc(); lead to a memory leak? You're not freeing it. Why in the world would I free that memory within the function itself? The whole point of allocating on the heap is so that the function returns a valid pointer. |
Author: | DtY [ Fri May 22, 2009 7:01 am ] |
Post subject: | RE:Translations from Turing |
Yes, I know if you freed it inside that function the pointer would be useless, but you never free that memory. |
Author: | DemonWasp [ Fri May 22, 2009 11:00 am ] |
Post subject: | RE:Translations from Turing |
He has a point. In your main(), you should be capturing that pointer and freeing it properly, but you're not. Probably a non-issue since the program ends immediately but better style regardless. |