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
code:
% This is a comment
put "Hello, world!"


C
code:
/* This a comment */

#include <stdio.h>

int main()
{
   puts("Hello, world!");
   return 0;
}


C++
code:
// This is a comment

#include <iostream>

int main()
{
   std::cout << "Hello, world!" << std::endl;
   return 0;
}


Java
code:
// This is a comment

import java.lang.*;
import java.io.*;

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


C#
code:
// This is a comment

using System;

public class HelloWorld
{
   public static void Main(string[] args)
   {
      Console.WriteLine("Hello, world!");
   }
}


D
code:
// This is a comment

import std.stream;

void main()
{
   stdout.writeLine("Hello, world!");
}


Perl
code:
# This a comment
print "Hello, world!\n";


Python
code:
# This a comment
print "Hello, world!"


Ruby
code:
# This a comment
puts "Hello, world!"


O'Caml
code:
(* 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/>");

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
code:
procedure sayHelloWorld
   put "Hello, world!"
end sayHelloWorld

sayHelloWorld


C
code:
#include <stdio.h>

void say_hello_world();

int main()
{
   say_hello_world();
   return 0;
}

void say_hello_world()
{
   puts("Hello, world!");
}


C++
code:
#include <iostream>

void say_hello_world();

int main()
{
   say_hello_world();
   return 0;
}

void say_hello_world()
{
   std::cout << "Hello, world!" << std::endl;
}


Java
code:
import java.lang.*;
import java.io.*;

public class HelloWorld {
   public static void main(String[] args) {
      sayHelloWorld();
   }

   private static void sayHelloWorld() {
      System.out.println("Hello, world!");
   }
}


C#
code:
using System;

public class HelloWorld
{
   public static void Main(string[] args)
   {
      SayHelloWorld();
   }

   private static void SayHelloWorld()
   {
      Console.WriteLine("Hello, world!");
   }
}


D
code:
import std.stream;

void main()
{
   sayHelloWorld();
}

void sayHelloWorld()
{
   stdout.writeLine("Hello, world!");
}


Perl
code:
say_hello_world;

sub say_hello_world {
   print "Hello, world!";
}


Python
code:
def say_hello_world():
   print "Hello, world!"

say_hello_world()


Ruby
code:
def say_hello_world
   puts "Hello, world!"
end

say_hello_world


O'Caml
code:
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();

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
code:
procedure sayHello(name : string)
   put "Hello, " + name + "!"
end sayHello

sayHello("Bob")


C
code:
#include <stdio.h>

void say_hello(const char * name);

int main()
{
   say_hello("Bob");
   return 0;
}

void say_hello(const char * name)
{
   printf("Hello, %s!\n", name);
}


C++
code:
#include <iostream>
#include <string>

void say_hello(std::string name);

int main()
{
   say_hello("Bob");
   return 0;
}

void say_hello(std::string name)
{
   std::cout << "Hello, " << name, "!" << std::endl;
}


Java
code:
import java.lang.*;
import java.io.*;

public class HelloWorld {
   public static void main(String[] args) {
      sayHello("Bob");
   }

   private static void sayHello(String name) {
      System.out.println("Hello, " + name + "!");
   }
}


C#
code:
using System;

public class HelloWorld
{
   public static void Main(string[] args)
   {
      SayHello("Bob");
   }

   private static void SayHello(string name)
   {
      Console.WriteLine("Hello, " + name + "!");
   }
}


D
code:
import std.stream;

void main()
{
   sayHello("Bob");
}

void sayHello(char[] name)
{
   stdout.writefln("Hello, %s!", name);
}


Perl
code:
say_hello("Bob");

sub say_hello {
   my $name = shift;
   print "Hello, $name!\n";
}


Python
code:
def say_hello(name):
   print "Hello, %(name)s!" % {"name": name}

say_hello("Bob")


Ruby
code:
def say_hello(name)
   puts "Hello, #{name}!"
end

say_hello("Bob")


O'Caml
code:
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");

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
code:
function greeting(name : string) : string
   result "Hello, " + name + "!"
end greeting

put greeting("Bob")


C
code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

char * greeting(const char * name);

int main()
{
   puts(greeting("Bob"));
   return 0;
}

char * greeting(const char * name)
{
   char * output = (char *)malloc(sizeof(char) * (9 + strlen(name)));
   sprintf(output, "Hello, %s!", name);
   return output;
}


C++
code:
#include <string>
#include <sstream>
#include <iostream>

std::string greeting(std::string name);

int main()
{
   std::cout << greeting("Bob") << std::endl;
   return 0;
}

std::string greeting(std::string name)
{
   return "Hello, " + name + "!";
}


Java
code:
import java.lang.*;
import java.io.*;

public class HelloWorld {
   public static void main(String[] args) {
      System.out.println(greeting("Bob"));
   }

   public static String greeting(String name) {
      return "Hello, " + name + "!";
   }
}


C#
code:
using System;

public class HelloWorld
{
   public static void Main(string[] args)
   {
      Console.WriteLine(Greeting("Bob"));
   }

   public static string Greeting(string name)
   {
      return "Hello, " + name + "!";
   }
}


D
code:
import std.stream;

void main()
{
   stdout.writeLine(greeting("Bob"));
}

char[] greeting(char[] name)
{
   return "Hello, " ~ name ~ "!";
}


Perl
code:
print greeting("Bob") . "\n";

sub greeting {
   my $name = shift;
   return "Hello, $name!";
}


Python
code:
def greeting(name):
   return "Hello, %(name)s!" % {"name": name}

print greeting("Bob")


Ruby
code:
def greeting(name)
   "Hello, #{name}!"
end

puts greeting("Bob")


O'Caml
code:
let greeting name =
   "Hello, " ^ name ^ "!"

print_endline (greeting "Bob")


Eiffel
code:
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/>");

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
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)


C
code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char * greeting(const char * name);

int main()
{
        puts("Your name is?");
        char * name = (char *)malloc(sizeof(char *));
        gets(name);
        puts(greeting(name));

        return 0;
}

char * greeting(const char * name)
{
        if (strcmp(name, "Clarence") == 0)
        {
                return "Hah ha!  Clarence...";
        }
        else if (strcmp(name, "Sid") == 0)
        {
                return "Sid?  What a maroon.";
        }
        else
        {
                char * output = (char *)malloc(sizeof(char *));
                sprintf(output, "Hello, %s!", name);
                return output;
        }
}


C++
code:
#include <iostream>
#include <string>

std::string greeting(std::string name);

int main()
{
   std::string name;
   std:cin >> name;
   std::cout << greeting(name) << std::endl;
   return 0;
}

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 ~ "!";
   }
}


Perl
code:
print "Your name is?\n";
chomp(my $name = <STDIN>);
print greeting($name) ."\n";

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.

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... Wink

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:

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 + "!";
   }

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
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)


C
code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

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++
code:
#include <string>
#include <iostream>

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 << greeting(firstName, lastName) << std::endl;
        return 0;
}

void getNames(std::string & firstName, std::string & lastNames)
{
        std::cout << "Your first name is?" << std::endl;
        std::cin >> firstName;
        std::cout << "And your last name is?" << std::endl;
        std::cin >> lastName;
}

std::string fullName(std::string firstName, std::string lastNames)
{
        return firstName + " " + lastName;
}

std::string greeting(std::string firstName, std::string lastNames)
{
        if (firstName == "Clarence" || lastName == "Clarence")
                return "Ha ha!  Clarence...";
        else if (lastName == "Sid" || lastName == "Sid")
                return "Sid?  What a maroon.";
        else
                return "Hello, " + fullName(firstName, lastName) + "!";
}


Java
code:
import java.lang.*;
import java.io.*;

public class HelloWorld {
        private static BufferedReader = new BufferedReader(
                new InputStreamReader(System.in));
               
        public static void main(String[] args) {
             String firstName = getFirstName();
                String lastName = getLastName();
             
             System.out.println(greeting(firstName, lastName));
        }
       
        private static String getFirstName() {
                System.out.println("Your first name is?");
                return keyboard.readLine();
        }

        private static String getLastName() {
                System.out.println("And your last name is?");
                return keyboard.readLine();
        }
       
        private String fullName(String firstName, String lastName) {
                return firstName + " " + lastName;
        }
       
        private String greeting(String firstName, String lastName) {
                if (firstName.equals("Clarence") || lastName.equals("Clarence"))
                        return "Ha ha!  Clarence...";
                else if (firstName.equals("Sid") || lastName.equals("Sid"))
                        return "Sid?  What a maroon.";
                else
                        return "Hello, " + fullName(firstName, lastName) + "!";
        }
}


C#
code:
using System;

public class HelloWorld
{
        public static void Main(string[] args)
        {
             string firstName = GetFirstName();
                string lastName = GetLastName();
             
             Console.WriteLine(Greeting(firstName, lastName));
        }
       
        private static string GetFirstName()
        {
                Console.WriteLine("Your first name is?");
                return Console.ReadLine();
        }

        private static string GetLastName()
        {
                Console.WriteLine("And your last name is?");
                return Console.ReadLine();
        }
       
        private string FullName(string firstName, string lastName)
        {
                return firstName + " " + lastName;
        }
       
        private string Greeting(string firstName, string lastName)
        {
                if (firstName.Equals("Clarence") || lastName.Equals("Clarence"))
                        return "Ha ha!  Clarence...";
                else if (firststName.Equals("Sid") || lastName.Equals("Sid"))
                        return "Sid?  What a maroon.";
                else
                        return "Hello, " + FullName(firstName, lastName) + "!";
        }
}


D
code:
import std.stream;

void main()
{
        char[] firstName, lastName;
       
        getNames(firstName, lastName);
        stdout.writeLine(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();
}

char[] fullName(char[] firstName, char[] lastName)
{
        return firstName ~ " " ~ lastName;
}

char[] greeting(char[] firstName, char[] lastName)
{
        if (firstName == "Clarence" || lastName == "Clarence")
                return "Ha ha!  Clarence...";
        else if (firstName == "Sid" || lastName == "Sid")
                return "Sid?  What a maroon.";
        else
                return "Hello, " ~ fullName(firstName, lastName) ~ "!";
}


Perl
code:
my ($first_name, $last_name) = get_names;

print greeting($first_name, $last_name) . "\n";

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 full_name(first_name, last_name):
   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)

first_name, last_name = get_names()
print greeting(first_name, last_name)

# or just:
# print greeting(get_names())


Ruby
code:
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

first_name, last_name = get_names
puts greeting(first_name, last_name)


O'Caml
code:
let get_first_name () =
   print_endline "Your first name is?";
   read_line ()

let get_last_name () =
   print_endline "And your last name is?";
   read_line ()

let full_name first_name last_name =
   first_name ^ " " ^ last_name

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

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

C:

char * greeting(const char * name)
{
   char * output = (char *)malloc(sizeof(char) * (9 + strlen(name)));
   sprintf(output, "Hello, %s!", name);
   return output;
}


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 Sad

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:
C:

char * greeting(const char * name)
{
   char * output = (char *)malloc(sizeof(char) * (9 + strlen(name)));
   sprintf(output, "Hello, %s!", name);
   return output;
}


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.


: