Most Recent Blog

King's View of the Battle

 Open GL model of a chess board.

Showing posts with label Hello World. Show all posts
Showing posts with label Hello World. Show all posts

March 13, 2011

Scala - Hello World

Scala's Hello World can be either similar to Java's, or simpler depending on how the problem is approached.  This article will approach the problem from both the Java style as well as the much more pure functional style.  Remember that Scala is a highly expressive functional language.  The second form of the hello world program will show this feature much more clearly.

Step 1: Create a Scala source file
Scala does not have the same restrictions on source file naming that Java has.  Therefore, you can give the Scala source file pretty much any name you like, so long as it ends in .scala.  The fist form of Hello World looks as:
__________________________________________________________________________________

object HelloWorld {
    def main(args: Array[String]) {
      println("Hello in scala from ecocrypt.blogspot.com")
    }
  }
__________________________________________________________________________________
Step 2: Compile the Scala Source
At a command prompt, type the scala compiler command:

prompt %
prompt % scalac HelloWorld.scala
prompt %
prompt % scalac HelloWorld.scala

The result of this will be to produce an output file called HelloWorld.class, this class file is a byte code version of the HelloWorld.scala source file.  This class file is identical to a Java class file, but has Scala class references embedded in it.  

Step 3: Running HelloWorld
At the command prompt, type:
prompt %
prompt % scala HelloWorld
Hello in scala from ecocrypt.blogspot.com
prompt %
prompt % scala HelloWorld
Hello in scala from ecocrypt.blogspot.com

Congratulate yourself!  You have successfully installed the Scala compiler and execution environment, created a simple scala source file compiled it to byte code, and run it inside of the Scala/Java Virtual Machine.

The exact same thing could be accomplished using a scala Application.  The source code for a Scala application based solution would look as:
__________________________________________________________________________________
object HelloWorld2 extends Application {
  println("Hello world in scala from ecocrypt.blogspot.com")
}
__________________________________________________________________________________



C++ - Hello World

C++'s Hello World is similar to Go's (golang), but there are some differences.  The similarity is that both C++ and Go create a file that is executed natively on the platform it is compiled to.  Also both C++ and Go follow a procedure of compile then link.  The main differences, for the Hello World program, are in the expressiveness and the syntax.  Writing hello world in C++ requires the following steps:

Step 1: Create a C++ source file
The source file for Hello World thus looks like the following:
__________________________________________________________________________________


#include <iostream>
using namespace std;

int main(int argc, const char** argv){
cout<<"Hello World in C++ from ecocrypt.blogspot.com"<<endl;
return 0;
}
__________________________________________________________________________________

Save this to any file name you like, but typically, you will have to have a .cpp extension on the end of the file name.  A good name to choose for this example would be helloworld.cpp.

Step 2: Compile the C++ Source
At a command prompt, type the c++ compiler-linker command:
prompt % c++ HelloWorld.cpp

This will typically do a 2 step process of compiling and linking.  Therefore no intermediate object files will be created.  The result of this will be to produce an output file called a.out, or a.exe. 

Step 3: Running HelloWorld
At the command prompt, type:
prompt % a.out      (or a.exe on windows)
Hello World in C++ from ecocrypt.blogspot.com

Congratulate yourself!  You have successfully installed a C++ compiler, and created a simple hello world executable.

Perl - Hello World

Writing hello world in perl is almost to simple to justify a post, however there are some caveats and for completeness of this group of posts, it will be included.
Step 1 Install Perl (5.x)
Once installed, make sure that you can access the perl executable.  If perl is not in the path, you will need to add it.

Step 2 Create a Perl Source file
______________________Source File____________________________
#!/opt/perl
print ("Hello World in Perl from ecocrypt.blogspot.com")
______________________End Source____________________________
save the source to a file called helloworld.pl  (or any other name of your choice)

Step 3 Tell Perl to execute the input file
prompt % perl helloworld.pl
Hello World in Perl from ecocrypt.blogspot.com

Congratulate yourself! You have successfully installed perl, create a perl source file, and executed it.

Java - Hello World

Java's Hello World is simpler than Go's (golang), but not as simple as Python's.  Also like python the output is quite portable from platform to platform.  Writing hello world in Java requires the following steps:

Step 1: Create a Java source file
There are actually quite a few caveats to this step alone because Java as some requirements that make it more restrictive  than any other language in this blog.  In Java, there everything is an "Object" and thus extends from java.lang.Object.  Therefore, you will have to create a full class definition even though we will not be instantiating any classes.  The source file for Hello World thus looks like the following:
__________________________________________________________________________________

public class HelloWorld extends java.lang.Object {
    public static void main(String args[]){
        System.out.println("Hello World in Java from ecocrypt.blogspot.com");
    }
}
__________________________________________________________________________________

There is another caveat in Java the class HelloWord must be stored in a file called HelloWorld.java. Picking another name will cause a compiler error.

Step 2: Compile the Java Source
At a command prompt, type the java compiler command:
prompt % javac HelloWorld.java

The result of this will be to produce an output file called HelloWorld.class, this class file is a byte code version of the HelloWorld.java source file.  Unlike golang, where the output of the linker can be directly executed, the java class file is not executable.  However, a java executable program and load the HelloWorld.class file, and execute the instructions.

Step 3: Running HelloWorld
At the command prompt, type:
prompt % java HelloWorld
Hello World in Java from ecocrypt.blogspot.com

Congratulate yourself!  You have successfully installed the JDK, created a simple java source file compiled it to byte code, and run it inside of the Java Virtual Machine.

March 11, 2011

Go Lang - Hello World

Starting a new language the first example is always Hello World.  Getting going with go-lang is only slightly different.  As of this writing, the current state of 'go-lang' is that you have to build the compiler first, and then write compile and run Hello World.  On UNIX/Linux you can do that pretty easily based on the instructions maintained on the golang.org website.  On windows the GOMINGW  seems to be maintaining good binary compatible version of go built with the mingw compiler collection on windows.

Step 1 Set your environment variables:

E.G. On my windows box I set:
set GOROOT=C:\golang\go
set GOOS=windows
set GOARCH=386
  (look in $GOROOT/pkg for applicable architecture and OS Options)


Step 2 Create a source file in the go language (a go source file):
Use a text editor or write a golang source file:
Sample Hello World GO Language source file:
________________________SOURCE FILE________________________________________


package main

import fmt "fmt"  // Package implementing formatted I/O.


func main() {

      fmt.Printf("Hello, world; from go to ecocrypt.blogspot.com \n")
}

________________________END SOURCE_________________________________________


Save this file to helloworld.go or any filename of your choice.

Step 3  Compile
prompt % 8g helloworld.go

Step 4 Link
prompt % 8l helloworld.go

Step 5 Run the newly created executable:
prompt % 8.out
Hello, world; from go to ecocrypt.blogspot.com

Congratulate Yourself!  You have created and run Hello World in Go.
You should now have a Go compiler successfully installed on your computer.


SOME COMMON ERRORS:
Using the wrong executable to compile go source:
prompt %  8c.exe helloworld.go

helloworld.go:1 not a function
helloworld.go:1 syntax error, last name: main

     This Error is caused because you are trying to compile a go source file with a c compiler.  8c is a c compiler embedded in the go distribution package.


Using the wrong architecture or os settings:
prompt % 8g.exe helloworld.go

helloworld.go:3: can't find import: fmt

Make sure you have set the go environment variables correctly:
Determining the GOOS, and GOARCH can be tricky, you will have to look in:
$GOROOT/pkg/
and there will be a directory of format $GOOS_$ARCH

E.G. On my windows box I set:

set GOROOT=C:\golang\go
set GOOS=windows
set GOARCH=386


March 9, 2011

Python - Hello World

Writing hello world in python is almost to simple to justify a post, however there are some caveats and for completeness of this group of posts, it will be included.

Step 1 Install Python 3.2 
Once installed, make sure that you can access the python executable.  If python is not in the path, you will need to add it.


Step 2 Create a Python Source file
______________________Source File____________________________
#!/opt/python
print ("Hello World in Python from ecocrypt.blogspot.com")
______________________End Source____________________________
save the source to a file called helloworld.py  (or any other name of your choice)


Step 3 Tell the Python Interrupter to execute the instructions in the file
prompt % python helloworld.py
Hello World in Python from ecocrypt.blogspot.com


This entire exercise could have been done from within the python interrupter.  To start the interpretor simply type 'python' at the command prompt this will give you a python interpretor prompt:


>>>
>>>print ("hello world in python from ecocyrpt.blogspot.com")
hello world in python from ecocyrpt.blogspot.com
>>>
type quit() to leave the interpretor


SOME COMMON ERRORS:
Using the wrong Python Syntax for version 3.2.

>>> print "Hello World"
  File "<stdin>", line 1
    print "Hello World"
                      ^
SyntaxError: invalid syntax
>>>

There were some syntax changes between python 2.7 and 3.2.
This fails because the print method is not enclosed in braces().
Try >>> print ("Hello World")