gdc
Implementation of programming language Dgdc is GNU D Compiler, which is included in GNU Compiler Collection.
Links:
Examples:
Hello, World!:
Example for versions D1, D2, gdc 0.24writef() and writefln() writes to standard output and interpret the first argument as a format string. They are roughly analogous to C’s printf(). writefln() automatically appends a newline. D2 adds write() and writeln() which do not interpret the first argument as a format string. However, they are not available in D1.
module hello;
import std.stdio;
int main()
{
writefln( "Hello, World!" );
return 0;
}
Factorial:
Example for versions D1, D2, gdc 0.24This example uses recursive factorial definition.
module factorial;
import std.stdio;
ulong recursive(ulong x)
{
return (x == 0 ? 1 : x * recursive( x - 1 ));
}
int main()
{
for (int i = 0; i < 17; ++i)
{
writefln("%s! = %s", i, recursive(i));
}
return 0;
}
Fibonacci numbers:
Example for versions D1, D2, gdc 0.24This example uses iterative definition of Fibonacci numbers.
module fibonacci;
import std.stdio;
ulong iterative(ulong x)
{
ulong prev1 = 1L;
ulong prev2 = 1L;
ulong result = x <= 2 ? 1L : 0L;
for ( ulong i = 3; i <= x; ++i )
{
result = prev1 + prev2;
prev1 = prev2;
prev2 = result;
}
return result;
}
int main()
{
for (uint i = 1; i < 17; i++)
{
writef("%s, ", iterative(i));
}
writefln("%s", "...");
return 0;
}
Fibonacci numbers:
Example for versions D1, D2, gdc 0.24This example uses recursive definition of Fibonacci numbers.
module fibonacci;
import std.stdio;
ulong recursive(ulong x)
{
return x <= 2 ? 1 : recursive( x - 2 ) + recursive( x - 1 );
}
int main()
{
for (uint i = 1; i < 17; i++)
{
writef("%s, ", recursive(i));
}
writefln("%s", "...");
return 0;
}
Factorial:
Example for versions D1, D2, gdc 0.24This example uses iterative factorial definition. Note the usage of foreach
loop.
module factorial;
import std.stdio;
ulong iterative(ulong x) {
ulong result = 1;
foreach (ulong count; 1..x+1)
result *= count;
return result;
}
int main() {
foreach (int i; 0..17)
writefln("%s! = %s", i, iterative(i));
return 0;
}
Comments
]]>blog comments powered by Disqus
]]>