Nimrod
- Appeared in:
- 2004
- Influenced by:
- Paradigm:
- Typing discipline:
- File extensions:
- .nim
- Versions and implementations (Collapse all | Expand all):
Nimrod is a modern programming language supporting multiple paradigms.
Nimrod (the name carries no particular meaning) was created in 2004 by Andreas Rumpf. The language design focuses on efficiency, expressiveness and elegance (in that order).
Language features:
- native code generation via compilation to C;
- fast non-tracing garbage collection plus direct memory management (pointers to these two kinds of memory are distinguishable);
- optimized compiler including dead code elimination;
- flexible syntax with powerful macro system;
- modern type system and set of built-in high-level data structures;
- converters from Pascal and C to Nimrod;
- support for object-oriented and functional programming;
- explicit typing with local type inference (figuring out the type of the variable from its initialization);
- statements are grouped using indentation, like in Python;
- the language is case-insensitive, and underscores in variable names (and in numbers) are ignored.
Elements of syntax:
Inline comments | # |
---|---|
Case-sensitivity | no |
Variable assignment | <varname> = <value> |
Variable declaration | var <varname> : <type> |
Variable declaration with assignment | var <varname> : <type> = <value> |
Block | (indentation) |
Function definition | proc f(arg1: type1, ..., argN: typeN): returnType = <block> |
Function call | f(arg1, ..., argN) |
If - then | if <condition>: <block> elif <condition>: <block> |
If - then - else | if <condition>: <block> elif <condition>: <block> else: <block> |
Loop forever | while true: <loopBody> |
While condition do | while <condition>: <loopBody> |
For each value in a numeric range, 1 increment | for i in countup(1, 10): <loopBody> |
For each value in a numeric range, 1 decrement | for i in countdown(1, 10): <loopBody> |
Links:
Examples:
Hello, World!:
Example for versions Nimrod 0.8.8echo "Hello, World!"
Factorial:
Example for versions Nimrod 0.8.8This 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:
Example for versions Nimrod 0.8.8This 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:
Example for versions Nimrod 0.8.8This 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:
Example for versions Nimrod 0.8.8This 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:
Example for versions Nimrod 0.8.8from 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:
Example for versions Nimrod 0.8.8This 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))
Comments
]]>blog comments powered by Disqus
]]>