Digital Mars D

Implementation of programming language D

Digital Mars D (sometimes abbraviated as dmd) is the reference compiler for the D Programming Language.

Examples:

Hello, World!:

Example for versions D1, D2, gdc 0.24

writef() 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.24

This 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.24

This 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.24

This 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.24

This 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;
}

Quadratic equation:

Example for versions D1, D2
import std.c.stdio;
import std.stdio;
import std.math;

int main() {
    int A, B, C;
    writef("A = ");
    scanf("%d", & A);
    if (A==0)
    {   writefln("Not a quadratic equation.");
        return 0;
    }
    writef("B = ");
    scanf("%d", & B);
    writef("C = ");
    scanf("%d", & C);
    A*=2;
    float D = B*B-2*A*C;
    if (D == 0)
    {   writefln("x = %f\n",-B*1.0/A);
        return 0;
    }
    if (D>0)
        writefln("x1 = %f\nx2 = %f",(-B+sqrt(D))/A,(-B-sqrt(D))/A);
    else    
        writefln("x1 = (%f, %f)\nx2 = (%f, %f)",-B*1.0/A,sqrt(-D)/A,-B*1.0/A,-sqrt(-D)/A);
    return 0;
}

CamelCase:

Example for versions D2

First line reads a line from standard input and converts it to lowercase. Second line replaces all non-letter characters with spaces (last parameter is attribute characters; g means that all matches of regular expression are replaced, not only the first one). Third line capitalizes words in string, removes leading and trailing spaces and replaces all sequences of spaces with a single space. Finally, the spaces are removed from the string, and the result is printed to standard output.

import std.stdio;
import std.string;
import std.regexp;
 
void main() {
    string text = tolower(readln());
    text = sub(text,"[^a-z]"," ","g");
    text = capwords(text);
    text = sub(text," ","","g");
    writeln(text);
}