Posted: 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.
(* This a comment *)
print_endline "Hello, world!"
Eiffel
code:
-- This is a comment
class HELLO_WORLD
creation {ANY} make
feature {ANY}
make is
do
std_output.put_string("Hello, world!")
std_output.put_new_line
end
end
Pascal
code:
(* This a comment *)
program HelloWorld;
begin
WriteLn('Hello, world!')
end.
Ada95
code:
-- This is a comment
with Ada.Text_IO; use Ada.Text_IO;
procedure Hello_World is
begin
Put_Line("Hello, world!");
end Hello_World;
Javascript
code:
// This is a comment
document.write("Hello, world!<br/>");
Sponsor Sponsor
wtd
Posted: Thu Oct 28, 2004 7:25 pm Post subject: (No 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
code:
procedure sayHelloWorld
put "Hello, world!"
end sayHelloWorld
let say_hello_world () =
print_endline "Hello, world!"
say_hello_world ()
Eiffel
code:
class HELLO_WORLD
creation {ANY} make
feature {ANY}
make is
do
say_hello_world
end
feature {NONE}
say_hello_world is
do
std_output.put_string("Hello, world!")
std_output.put_new_line
end
end
Pascal
code:
program HelloWorld;
var
procedure SayHelloWorld;
begin
WriteLn('Hello, world!')
end;
begin
SayHelloWorld
end.
Ada
code:
with Ada.Text_IO; use Ada.Text_IO;
procedure Hello_World is
procedure Say_Hello_World is
begin
Put_Line("Hello, world!");
end Say_Hello_World;
begin
Say_Hello_World;
end Hello_World;
Javascript
code:
function sayHelloWorld() {
document.write("Hello, world!");
}
sayHelloWorld();
Andy
Posted: Thu Oct 28, 2004 7:33 pm Post subject: (No subject)
woa there is a D?
wtd
Posted: Thu Oct 28, 2004 7:41 pm Post subject: (No subject)
It was created by C and C++ compiler vendor Digital Mars, to address what they felt were shortcomings in C++.
wtd
Posted: Thu Oct 28, 2004 10:20 pm Post subject: (No 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
code:
procedure sayHello(name : string)
put "Hello, " + name + "!"
end sayHello
let say_hello name =
print_endline ("Hello, " ^ name ^ "!")
say_hello "Bob"
Eiffel
code:
class HELLO_WORLD
creation {ANY} make
feature {ANY}
make is
do
say_hello("Bob")
end
feature {NONE}
say_hello(name : STRING) is
do
std_output.put_string("Hello, " + name + "!")
std_output.put_new_line
end
end
Pascal (This one should work, but FreePascal is giving me a syntax error)
code:
program Strings;
var
procedure SayHello(Name : String);
begin
WriteLn(Name)
end;
begin
SayHello('Bob')
end.
Ada - Strings in Ada are tricky enough that I've left this one out
Javascript
code:
function sayHello(name) {
document.write("Hello, " + name + "!");
}
sayHello("Bob");
wtd
Posted: Thu Oct 28, 2004 11:36 pm Post subject: (No 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
code:
function greeting(name : string) : string
result "Hello, " + name + "!"
end greeting
class HELLO_WORLD
creation {ANY} make
feature {ANY}
make is
do
std_output.put_string(greeting("Bob"))
std_output.put_new_line
end
feature {NONE}
greeting(name : STRING) : STRING is
do
Result := "Hello, " + name + "!"
end
end
Pascal
code:
program HelloWorld;
var
function Greeting(Name : String) : String;
begin
Greeting := 'Hello, ' + name + '!'
end;
begin
WriteLn(Greeting('Bob'))
end.
Javascript
code:
function greeting(name) {
return "Hello, " + name + "!";
}
document.write(greeting("Bob") + "<br/>");
wtd
Posted: Fri Oct 29, 2004 12:46 am Post subject: (No 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
code:
procedure greeting(name : string) : string
if name = "Clarence" then
result "Ha ha! Clarence..."
elsif name = "Sid" then
result "Sid? What a maroon."
else
result "Hello, " + name + "!"
end if
end greeting
put "Your name is?"
var name : string
get name
put greeting(name)
std::string greeting(std::string name)
{
if (name == "Clarence")
return "Ha ha! Clarence...";
else if (name == "Sid")
return "Sid? What a maroon.";
else
return "Hello, " + name + "!";
}
Java
code:
import java.lang.*;
import java.io.*;
public class HelloWorld {
private static BufferedReader keyboard = new BufferedReader(
new InputStreamReader(System.in));
public static void main(String[] args) {
System.out.println("Your name is?");
String name = keyboard.readLine();
System.out.println(greeting(name));
}
private static String greeting(String name) {
if (name.equals("Clarence"))
return "Ha ha! Clarence...";
else if (name.equals("Sid"))
return "Sid? What a maroon.";
else
return "Hello, " + name + "!";
}
}
C#
code:
using System;
public class HelloWorld
{
public static void Main(string[] args)
{
Console.WriteLine("Your name is?");
string name = Console.ReadLine();
Console.WriteLine(Greeting(name));
}
private static string Greeting(string name)
{
if (name.Equals("Clarence"))
return "Ha ha! Clarence...";
else if (name.Equals("Sid"))
return "Sid? What a maroon.";
else
return "Hello, " + name + "!";
}
}
D
code:
import std.stream;
void main()
{
stdout.writeLine("Your name is?");
char[] name = stdin.readLine();
stdout.writeLine(greeting(name));
}
char[] greeting(char[] name)
{
switch (name)
{
case "Clarence":
return "Ha ha! Clarence...";
case "Sid":
return "Sid? What a maroon.";
default:
return "Hello, " ~ name ~ "!";
}
}
sub greeting {
my $name = shift;
if ($name eq "Clarence") {
return "Ha ha! Clarence...";
} else if ($name eq "Sid") {
return "Sid? What a maroon.";
} else {
return "Hello, $name!";
}
}
Python
code:
def greeting(name):
if name is "Clarence":
return "Ha ha! Clarence..."
elif name is "Sid":
return "Sid? What a maroon."
else:
return "Hello, %(name)s!" % {"name": name}
print "Your name is?"
name = readline()
print greeting(name)
Ruby
code:
def greeting(name)
case name
when "Clarence"
"Ha ha! Clarence..."
when "Sid"
"Sid? What a maroon."
else
"Hello, #{name}!"
end
end
puts "Your name is?"
name = gets
puts greeting(name)
O'Caml
code:
let greeting = function
"Clarence" -> "Ha ha! Clarence..."
| "Sid" -> "Sid? What a maroon."
| name -> "Hello, " ^ name ^ "!"
print_endline "Your name is?"
let name = read_line () in
print_endline (greeting name)
Eiffel
code:
class HELLO_WORLD
creation {ANY} make
feature {ANY}
make is
local
name : STRING
do
std_output.put_string("Your name is?")
std_output.put_new_line
std_input.read_line
name := std_input.last_string
std_output.put_string(greeting(name))
std_output.put_new_line
end
feature {NONE}
greeting(name : STRING) : STRING is
do
inspect name
when "Clarence" then
Result := "Ha ha! Clarence..."
when "Sid" then
Result := "Sid? What a maroon."
else
Result := "Hello, " + name + "!"
end
end
end
Pascal
code:
program HelloWorld;
var
function Greeting(Name : String) : String;
begin
if Name = 'Clarence' then
Greeting := 'Ha ha! Clarence...'
else if Name = "Sid" then
Greeting := 'Sid? What a maroon.'
else
Greeting := 'Hello, ' + Name + '!'
end;
Name : String
begin
WriteLn('Your name is?');
Get(Name);
WriteLn(Greeting(Name))
end.
Delos
Posted: Fri Oct 29, 2004 8:32 am Post subject: (No subject)
As per usual wtd, quite awsome. BTW, is there any particular reason why you know the basics in so many languages? Pastime?
Sponsor Sponsor
wtd
Posted: Fri Oct 29, 2004 1:33 pm Post subject: (No 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...
rizzix
Posted: Fri Oct 29, 2004 3:36 pm Post subject: (No 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:
code:
public static String greeting(String name) {
return "Hello, " + name + "!";
}
and u can do something similar for c#
code:
public static string Greeting(string name)
{
return "Hello, " + name + "!";
}
wtd
Posted: Fri Oct 29, 2004 5:34 pm Post subject: (No subject)
I probably should have used the concatenation operator. I'll edit it to simplify those examples.
wtd
Posted: Fri Oct 29, 2004 9:27 pm Post subject: (No subject)
Let's extend the previous example to greet someone with a fitrst and last name.
Turing
code:
procedure getNames(var firstName, lastName : string)
put "Your first name is?"
get firstName
put "And your last name is?"
get lastName
end getNames
function greeting(firstName, lastName : string) : string
if firstName = "Clarence" or lastName = "Clarence" then
result "Ha ha! Clarence..."
elsif firstName = "Sid" or lastName = "Sid" then
result "Sid? What a maroon."
else
result "Hello, " + firstName + " " + lastName + "!"
end if
end greeting
var firstName, lastName : string
getNames(firstName, lastName)
put greeting(firstName, lastName)
void getNames(out char[] firstName, out char[] lastName)
{
stdout.writeLine("Your first name is?");
firstName = stdin.readLine();
stdout.writeLine("And your last name is?");
lastName = stdin.readLine();
}
sub get_names {
print "Your first name is?\n";
my $first_name = <>;
print "And your last name is?\n";
my $last_name = <>;
return ($first_name, $last_name);
}
sub full_name {
my ($first_name, $last_name) = @_;
return "$first_name $last_name";
}
sub greeting {
my ($first_name, $last_name) = @_;
if ($first_name eq "Clarence" or $last_name eq "Clarence") {
return "Ha ha! Clarence...";
elsif ($first_name eq "Sid" or $last_name eq "Sid") {
return "Sid? What a maroon.";
else
return "Hello, " . full_name($first_name, $last_name) . "!";
}
Python
code:
def get_names():
print "Your first name is?"
first_name = readline()
print "And your last name is?"
last_name = readline()
return first_name, last_name
def greeting(first_name, last_name):
if first_name is "Clarence" or last_name is "Clarence":
return "Ha ha! Clarence..."
elif first_name is "Sid" or last_name is "Sid":
return "Sid? What a maroon."
else:
return "Hello, %s!" % full_name(first_name, last_name)
def get_names
puts "Your first name is?"
first_name = gets
puts "And your last name is?"
last_name = gets
[first_name, last_name]
end
def full_name(first_name, last_name)
"#{first_name} #{last_name}"
end
def greeting(first_name, last_name)
if first_name == "Clarence" or last_name == "Clarence"
"Ha ha! Clarence..."
elsif first_name == "Sid" or last_name == "Sid"
"Sid? What a maroon."
else
"Hello, #{full_name(first_name, last_name)}!"
end
end
let greeting first_name last_name =
if first_name = "Clarence" or last_name = "Clarence" then
"Ha ha! Clarence..."
else if first_name = "Sid" or last_name = "Sid" then
"Sid? What a maroon."
else
"Hello, " ^ (full_name first_name last_name) ^ "!"
let first_name = get_first_name and last_name = get_last_name in
print_endline (greeting first_name last_name)
Eiffel
code:
class HELLO_WORLD
creation {ANY} make
feature {NONE}
get_first_name : STRING is
do
std_output.put_string("Your first name is?")
std_output.put_new_line
std_input.read_line
Result := std_input.last_string
end
get_last_name : STRING is
do
std_output.put_string("And your last name is?")
std_output.put_new_line
std_input.read_line
Result := std_input.last_string
end
full_name(first_name, last_name : STRING) : STRING is
do
Result := first_name + " " + last_name
end
greeting(first_name, last_name : STRING) : STRING is
do
if first_name = "Clarence" or last_name = "Clarence" then
Result := "Ha ha! Clarence..."
elsif first_name = "Sid" or last_name = "Sid" then
Result := "Sid? What a maroon."
else
Result := "Hello, " + full_name(first_name, last_name) + "!"
end
end
feature {ANY}
make is
local
first_name, last_name : STRING
do
first_name := get_first_name
last_name := get_last_name
std_output.put_string(greeting(first_name, last_name))
std_output.put_new_line
end
end
zomg
Posted: Wed Nov 17, 2004 1:31 pm Post subject: (No subject)
i never realized there was this many programmin languages
i only heard of c, c++, java, vb, python, turing
wtd
Posted: Wed Nov 17, 2004 7:03 pm Post subject: (No subject)
shadow master wrote:
i never realized there was this many programmin languages