3.1 Example Programs

3.1.1 Hello, World

The mandatory 'Hello World' program is a one-liner in Euphoria.

puts(1, "Hello, World\n")

The built-in routine puts() does the job of displaying text on a screen. It requires two arguments. The first argument, 1, directs the output to STDOUT or the console. The second argument, is a string of text that will be output.

The result is:

Hello, World

3.1.2 Sorting

The following is an example of a more useful Euphoria program.

include std/console.e
sequence original_list

function merge_sort(sequence x)
-- put x into ascending order using a recursive merge sort
    integer n, mid
    sequence merged, a, b

    n = length(x)
    if n = 0 or n = 1 then
        return x  -- trivial case
    end if

    mid = floor(n/2)
    a = merge_sort(x[1..mid])       -- sort first half of x
    b = merge_sort(x[mid+1..n])     -- sort second half of x

    -- merge the two sorted halves into one
    merged = {}
    while length(a) > 0 and length(b) > 0 do
        if compare(a[1], b[1]) < 0 then
            merged = append(merged, a[1])
            a = a[2..length(a)]
            merged = append(merged, b[1])
            b = b[2..length(b)]
        end if
    end while
    return merged & a & b  -- merged data plus leftovers
end function

procedure print_sorted_list()
-- generate sorted_list from original_list
    sequence sorted_list
    original_list = {19, 10, 23, 41, 84, 55, 98, 67, 76, 32}
    sorted_list = merge_sort(original_list)
    for i = 1 to length(sorted_list) do
    	display("Number [] was at position [:2], now at [:2]", 
    	        {sorted_list[i], find(sorted_list[i], original_list), i}
    end for
end procedure

print_sorted_list()     -- this command starts the program

The above example contains a number of statements that are processed in order.

include std/console.e
This tells Euphoria that this application needs access to the public symbols declared in the file 'std/console.e'. This is referred to as a library file. In our case here, the application will be using the display() routine from console.e.
sequence original_list
This declares a variable that is not public but is accessible from anywhere in this file. The datatype for the variable is a sequence, which is a variable-length array, and whose symbol name is original_list.
function merge_sort(sequence x) ... end function
This declares and defines a function routine. Functions return values when called. This function must be passed a single parameter when called - a sequence.
procedure print_sorted_list() ... end procedure
This declares and defines a procedure routine. Procedures never return values when called. This procedure must not be passed any parameters when called.
This calls the routine called print_sorted_list.
The output from the program will be:
Number 10 was at position  2, now at  1
Number 19 was at position  1, now at  2
Number 23 was at position  3, now at  3
Number 32 was at position 10, now at  4
Number 41 was at position  4, now at  5
Number 55 was at position  6, now at  6
Number 67 was at position  8, now at  7
Number 76 was at position  9, now at  8
Number 84 was at position  5, now at  9
Number 98 was at position  7, now at 10

Note that merge_sort() will just as easily sort any list of data items ...

{1.5, -9, 1e6, 100}
{"oranges", "apples", "bananas"}

This example is stored as euphoria\tutorial\example.ex. This is not the fastest way to sort in Euphoria. Go to the euphoria\demo directory and type

eui allsorts

to compare timings on several different sorting algorithms for increasing numbers of objects.

For a quick tutorial example of Euphoria programming, see euphoria\demo\bench\filesort.ex.

3.1.3 What to Do?

Now that you have installed Euphoria, here are some things you can try: Run the Demo Programs

Run each of the demo programs in the demo directory. You just type eui <program name>. An example of running the demos in a console

eui buzz

You can also double-click on a .ex or .exw file from Windows as file associations have been setup during the installation process. Edit Sample Files

Use the Euphoria editor, ed, to edit a Euphoria file. Notice the use of colors. You can adjust these colors along with the cursor size and many other "user-modifiable" parameters by editing constant declarations in ed.ex. Use Esc q to quit the editor or Esc h for help. There are several, even better, Euphoria-oriented editors in The Archive. If you use a more sophisticated text editor, many have a highlighter file for Euphoria. You will find it either on the Archive or on the community page for that editor. Check the wiki for more information about Euphoria editors. Benchmark

Create some new benchmark tests. See demo\bench. Do you get the same speed ratios as we did in comparison with other popular languages? Report your findings on the forum. Read the Manual

Read the manual in html\index.html by double-clicking it. If you have a specific question, type at the console:

guru word

The guru program will search all the .doc files, example programs, and other files, and will present you with a sorted list of the most relevant chunks of text that might answer your enquiry. Visit the EuForum

Euphoria Discussion Forum Trace a Demo

Try running a Euphoria program with tracing turned on. Add:

with trace

at the beginning of any Euphoria source file. Run the Tutorial Programs

Run some of the tutorial programs in euphoria\tutorial. Modify the Tutorial Programs

Try modifying some of the demo programs.

First some simple modifications (takes less than a minute):


What if there were 100 C++ ships in Language Wars? What if sb.ex had to move 1000 balls instead of 125? Change some parameters in polygon.ex. Can you get prettier pictures to appear? Add some funny phrases to buzz.ex.


Then, some slightly harder ones (takes a few minutes):

Define a new function of x and y in plot3d.ex.


Then a challenging one (takes an hour or more):

Set up your own customized database by defining the fields in mydata.ex.


Then a major project (several days or weeks):

Write a smarter 3D TicTacToe algorithm. Write Your Own

Try writing your own program in Euphoria. A program can be as simple as:

? 2+2

Remember that after any error you can simply type: ed to jump into the editor at the offending file and line.

Once you get used to it, you'll be developing programs much faster in Euphoria than you could in Perl, Java, C/C++ or any other language that we are aware of.