D1

Version of implementation Digital Mars D of programming language D

D1 is the first version of the D Programming Language.

Examples:

Hello, World! - D (80):

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 - D (101):

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 - D (102):

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 - D (139):

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 - D (140):

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 - D (217):

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