Nimrod 0.8.8

Version of implementation Nimrod of programming language Nimrod

A version released on March 14th, 2010.

Examples:

Hello, World! - Nimrod (457):

echo "Hello, World!"

Factorial - Nimrod (458):

This example uses recursive factorial definition.

proc factorial(n: int): int64 =
  if n == 0:
    result = 1
  else:
    result = n * factorial(n - 1)

for i in countup(0,16):
  echo i, "! = ", factorial(i)

Factorial - Nimrod (459):

This example uses iterative factorial calculation. Note that you can’t just omit int64 in variable definition, since its type will be deduced as integer, and it will overflow on 13!.

var f: int64 = 1
for i in countup(0,16):
  echo i, "! = ", f
  f = f * (i + 1)

Fibonacci numbers - Nimrod (460):

This example uses iterative calculation of Fibonacci numbers. A single var keyword can declare several variables if they are indented to form a block. Note that variable type can be omitted from the declaration only if it is initialized immediately; Nimrod uses local type inference, not global one.

for i in 1..16 is an alternate form of writing countup loop. & is string concatenation operator, and $ is number-to-string conversion.

var
  f1 = 1
  f2 = 1
  f3: int
  res = ""
for i in 1..16:
  res = res & $f1 & ", "
  f3 = f1 + f2
  f1 = f2
  f2 = f3
echo res & "..."

Fibonacci numbers - Nimrod (461):

This example uses Binet’s formula to calculate the numbers. Addition of epsilon value before rounding is necessary to get exact integer values.

from math import sqrt, pow, round

proc fibonacci(n: int): int =
  var phi: float64 = (1.0 + sqrt(5.0)) / 2.0
  return round((pow(phi, float64(n)) - pow(-phi, -float64(n))) / sqrt(5.0) + 0.0001)

var res = ""
for i in 1..16:
  res = res & $fibonacci(i) & ", "
echo res & "..."

Quadratic equation - Nimrod (462):

from math import sqrt
from strutils import parseFloat

var A = parseFloat(readLine(stdin))
if A == 0.0:
  echo "Not a quadratic equation."
else:
  var B = parseFloat(readLine(stdin))
  var C = parseFloat(readLine(stdin))
  var D = B * B - 4.0 * A * C
  if D == 0.0:
    echo "x = ", -0.5 * B / A
  elif D > 0.0:
    echo "x1 = ", -0.5 * (B - sqrt(D)) / A
    echo "x2 = ", -0.5 * (B + sqrt(D)) / A
  else:
    echo "x1 = (", -0.5 * B / A, ", ",  0.5 * sqrt(-D) / A, ")"
    echo "x2 = (", -0.5 * B / A, ", ", -0.5 * sqrt(-D) / A, ")"

CamelCase - Nimrod (463):

This example uses regular expressions. In Nimrod they are implemented using PRCE (Perl-Compatible Regular Expressions) library written in C.

from strutils import toLower, capitalize, join
import re

var text = toLower(readLine(stdin))
var words = split(text, re"[^a-z]+")
echo join(words.each(capitalize))