
-----------------------------------
wtd
Thu Oct 28, 2004 7:00 pm

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
% This is a comment
put "Hello, world!"

C
/* This a comment */

#include 

int main()
{
   puts("Hello, world!");
   return 0;
}
   
C++
// This is a comment

#include 

int main()
{
   std::cout  "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
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
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
Fri Oct 29, 2004 8:32 am


-----------------------------------
As per usual wtd, quite awsome.  BTW, is there any particular reason why you know the basics in so many languages?  Pastime?

-----------------------------------
wtd
Fri Oct 29, 2004 1:33 pm


-----------------------------------
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
Fri Oct 29, 2004 3:36 pm


-----------------------------------
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..


   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:


   public static String greeting(String name) {
      return "Hello, " + name + "!";
   }
 

and u can do something similar for c#


   public static string Greeting(string name)
   {
       return "Hello, " + name + "!";
   } 


-----------------------------------
wtd
Fri Oct 29, 2004 5:34 pm


-----------------------------------
I probably should have used the concatenation operator.  I'll edit it to simplify those examples.

-----------------------------------
wtd
Fri Oct 29, 2004 9:27 pm


-----------------------------------
Let's extend the previous example to greet someone with a fitrst and last name.

Turing
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)

C
#include 
#include 
#include 

void getNames(char * firstName, char * lastName);
char * fullName(char * firstName, char * lastName);
char * greeting(char * firstName, char * lastName);

int main()
{
	char * firstName = (char *)malloc(sizeof(char *));
	char * lastName  = (char *)malloc(sizeof(char *));
	
	getNames(firstName, lastName);
	puts(greeting(firstName, lastName));

	return 0;
}

void getNames(char * firstName, char * lastName)
{
	puts("Your first name is?");
	gets(firstName);
	puts("And your second name is?");
	gets(lastName);
}

char * fullName(char * firstName, char * lastName)
{
	char * output = (char *)malloc(sizeof(char *));
	sprintf(output, "%s %s", firstName, lastName);
	return output;
}

char * greeting(char * firstName, char * lastName)
{
	if (strcmp(firstName, "Clarence") == 0 || strcmp(lastName, "Clarence") == 0)
		return "Ha ha!  Clarence...";
	else if (strcmp(firstName, "Sid") == 0 || strcmp(lastName, "Sid") == 0)
		return "Sid?  What a maroon.";
	else
	{
		char * output = (char *)malloc(sizeof(char *));
		sprintf(output, "Hello, %s!", fullName(firstName, lastName));
		return output;
	}
}

C++
#include 
#include 

void getNames(std::string & firstName, std::string & lastNames);
std::string fullName(std::string firstName, std::string lastNames);
std::string greeting(std::string firstName, std::string lastNames);

int main()
{
	std::string firstName, lastName;
	getNames(firstName, lastName);
	std::cout 