General musings on programming languages, and Java.

Saturday, September 26, 2009

Which language shall we learn?

Hi Jose,

We decided, while drinking an overly-priced red wine the other night, that I'd help you to learn how to program, but without making it sound complicated. So, I thought I'd show you a few different language syntaxes and let you choose.

You can do anything you want with any of these languages, so feel free to pick the prettiest or whichever makes you laugh, or use whatever other criteria you feel like.

I chose the top-10 current mainstream languages, with the exception of PHP, which is too focused on one kind of task, and added Scala, Haskell and Erlang, because I like them.


1.
import java.util.Scanner;

public class Main {
  public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Please enter your name.");  
    String line = scanner.nextLine();
    System.out.println("Nice to meet you, " + line + ".");
  }
}

2.
#include <stdio.h>

int main(int argc, char **argv) {
  char name[256];

  printf("Please enter your name.\n");
  scanf("%s", name);
  printf("Nice to meet you, %s.\n", name);
  return 0;
}

3.
#include <iostream>
using namespace std;

int main() {
  string name;
  cout << "Please enter your name.\n");
  getline(cin, name);
  cout << "Nice to meet you, " << name << ".\n";
  return 0;
}

4.
Module example
  Public Sub Main()
    Dim line As String
    Console.WriteLine("Please enter your name.")
    line = Console.ReadLine()
    Console.WriteLine("Nice to meet you, " + line + ".")
  End Sub
End Module

5.
#!/usr/bin/perl -w
use strict;
print "Please enter your name.\n";
my $name = <>;
chomp $name;
print "Nice to meet you, $name.\n";

6.
using System;
class Hello {
  static void Main() {
    Console.WriteLine("Please enter your name.\n");
    var name = Console.ReadLine();
    Console.WriteLine("Nice to meet you, " + name + ".\n");
  }
}

7.
name = raw_input("Please enter your name: ")
print "Nice to meet you, ", name, "."

8.
importPackage(java.util);
scanner = new Scanner(System['in']);
System.out.println("Please enter your name.");
name = scanner.nextLine();
System.out.println("Nice to meet you, " + line + ".");

9.
puts "Please enter your name."
name = gets.chomp
puts "Nice to meet you, " + name

10.
main = do
  putStrLn "Please enter your name."
  name <- getLine
  putStrLn ("Nice to meet you, " ++ name ++ ".")

11.
val name = Console.readLine("Please enter your name.")
println("Nice to meet you, " + name + ".")

12.
Name = get_line("Please enter your name.").
put_chars("Nice to meet you, " ++ Name).

11 comments:

Jorge Ortiz said...

Scala has a more idiomatic (and cross-platform) solution:

val name = Console.readLine("Please enter your name.")
Console.println("Nice to meet you, "+name+".")

Anonymous said...

7

Daniel said...

The perl example needed not "use strict". And, because you used it, it won't run, as you did not qualify "$name" with "our" or "my".

Also, it would be better with "readline" instead of "<>" -- since that's what's used in the other examples.

Daniel said...

I meant "$line" instead of "$name". Also, you'll need to chomp it.

Ricky Clarkson said...

Thanks, Jorge, I changed the Scala version.

Daniel, I used to teach Perl programming for about 6 weeks per year, but with someone else providing tutorial notes; he never used strict or warnings, and students struggled. After a couple of weeks I used to write them in huge letters on the whiteboard and I'll never skip them again.

I added 'my' and chomp, but I won't be removing use strict;!

Daniel said...

I always use "strict" myself, but, strictly speaking :-), it is not necessary and the examples don't seem to place any emphasis on good coding standards.

However, I can understand your feelings given that background! :-)

Unknown said...

Should be
 var name = Console.ReadLine();
instead of
 var line = Console.ReadLine();
in 6, I think.

Ricky Clarkson said...

Thanks Jim, fixed.

Jose De Luca said...

Hi Ricky,



I've reached the conclusion that the ‘easiest’ language to my eyes and brain might be number “9”.



Do you think that with my chosen option I might be able to understand a tiny winy bit how to program?



Te amo,



Jose

Helen Hunt said...

The Python and Scala examples are my favourites - they're easier to understand for a beginner.

marek_k said...

in Ruby You can write:

p("Enter name: ")
p("Hi, " + gets.chomp)

Blog Archive

About Me

A salsa dancing, DJing programmer from Manchester, England.