Euphoria

Influenced by:
Paradigm:
Typing discipline:
File extensions:
ex,e,exw
Versions and implementations (Collapse all | Expand all):
Programming language

Euphoria is a general purpose procedural language. Its features include a simple type system, garbage collector, and minimal use of punctuation symbols.

It has only two fundamental types — the scalar (atom) and vector (sequence). There is also a dynamic type (object). An object can hold either an atom or a vector, an atom can hold a single number, and the vector is a variable length array of zero or more objects. There is also a helper type (integer) which is a restricted type of atom used for performance when floating point values are not required.

All other data structures can be derived from these four built-in types. For example, a string is a sequence of positive integers. By default, strings are UTF-32 encoding.

Elements of syntax:

Inline comments --
Nestable comments /* */
Case-sensitivity Yes
Variable identifier regexp [a-zA-Z_][a-zA-Z0-9_]*
Function identifier regexp [a-zA-Z_][a-zA-Z0-9_]*
Variable assignment =
Variable declaration TYPE IDENTIFIER
Variable declaration with assignment TYPE IDENTIFIER = EXPRESSION
Grouping expressions ( )
Block BLOCKTYPE do ... end BLOCKTYPE
Physical (shallow) equality =
Physical (shallow) inequality !=
Deep equality =
Deep inequality !=
Comparison < <= > >=
Function definition function IDENTIFIER ( [ARGLIST] ) ... end function
Function call IDENTIFIER( [PARMLIST] )
Function call with no parameters IDENTIFIER()
If - then if BOOLEXPR then ... end if
If - then - else if BOOLEXPR then ... else ... end if
Loop forever while 1 do ... end while
While condition do while CONDITION do ... end while
Do until condition loop do ... until CONDITION
For each value in a numeric range, 1 increment for i = 1 to N do ... end for
For each value in a numeric range, 1 decrement for i = N to 1 by -1 do ... end for

Examples:

Hello, World!:

Example for versions Euphoria v3.1.1, Euphoria v4

The routine puts() writes the second argument (a string) to the device identified by the first argument. There are three predefined devices: 0 is STDIN, 1 is STDOUT, and 2 is STDERR.

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

Factorial:

Example for versions Euphoria v3.1.1, Euphoria v4

This example uses recursive factorial definition.

function factorial(integer n)
  atom result

  if n <= 1 then
    result =  1
  else
    result = n * factorial(n-1)
  end if

  return result
end function

for i = 0 to 16 do
 printf(1, "%d! = %d\n", {i, factorial(i)})
end for

Factorial:

Example for versions Euphoria v3.1.1, Euphoria v4

This example uses iterative factorial definition.

function factorial(integer n)
  atom result = 1

  if n < 1 then
    return 1
  end if

  for i = 2 to n do
    result *= i
  end for

  return result
end function

for i = 0 to 16 do
 printf(1, "%d! = %d\n", {i, factorial(i)})
end for

Fibonacci numbers:

Example for versions Euphoria v3.1.1, Euphoria v4

This example uses recursive definition of Fibonacci numbers.

function fibonacci(integer n)
  atom result = 1

  if n <= 2 then
    result =  1
  else
    result = fibonacci(n-1) + fibonacci(n-2)
  end if

  return result
end function

for i = 1 to 16 do
  printf(1, "%d, ", fibonacci(i))
end for
puts(1, "...\n")

Fibonacci numbers:

Example for versions Euphoria v3.1.1, Euphoria v4

This example uses iterative definition of Fibonacci numbers.

function fibonacci(integer n)
  sequence result

  result = repeat(1, n)
  for i = 3 to n do
  	result[i] = result[i-1] + result[i-2]
  end for
  return result
end function

sequence ans = fibonacci(16)
for i = 1 to length(ans) do
  printf(1, "%d, ", ans[i])
end for
puts(1, "...\n")