Phix programming examples.
Hello world, character console
puts(1,"Hello world!\n") {} = wait_key()
The above complete program displays a message and waits for a character to be entered on a standard (old-school) character console, and looks like this:
While few people still write character console applications, they remain a great way to learn programming, and are ideal for quick and dirty one-off programs.
Hello world, gui popup
{} = message_box("world!","Hello",MB_OK)
The above complete program displays a message in a more modern fashion, and looks like this:
A simple sorting routine
function merge_sort(sequence x) -- put x into ascending order using a recursive merge sort integer midpoint sequence merged, first_half, second_half if length(x)<=1 then return x -- trivial case end if midpoint = floor(length(x)/2) first_half = merge_sort(x[1..midpoint]) second_half = merge_sort(x[midpoint+1..$]) -- merge the two sorted halves into one merged = {} while length(first_half)>0 and length(second_half)>0 do if first_half[1]<=second_half[1] then merged = append(merged, first_half[1]) first_half = first_half[2..$] else merged = append(merged, second_half[1]) second_half = second_half[2..$] end if end while -- result is the merged data plus any leftovers return merged & first_half & second_half end function sequence list = {9, 10, 3, 1, 4, 5, 8, 7, 6, 2} ? merge_sort(list)
The above example delares a function, a sequence, and then invokes the function and displays the results. It also demonstrates how sequences (flexible arrays) are the real powerhouse of Phix. Obviously the above is for illustration purposes only, in practice you would normally just use the built-in sort() routine.
The output from the program is:
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
The distribution also includes Edita, a full featured open source programmers editor, which in total is around 45,000 lines of code, some 130 demo programs, as well as pgui, which allows you to list, sort, filter, and run those 130 demos.