[ create a new paste ] login | about

Link: http://codepad.org/YJjb1t91    [ raw code | fork ]

D, pasted on Feb 26:
import std.stdio, std.traits, std.range, std.algorithm;

void truthTable(size_t N)
               (immutable bool function(in bool[N]) pure nothrow fun,
                in string expression, in char[N] vars...)
if (N > 0) {
    writefln("\n\n%( %2c %)     %s\n", vars, expression);
    bool[N] bits;
    foreach (immutable i; 0 .. 2 ^^ N) {
        N.iota.map!(j => !!(i & (1 << j))).copy(bits[]);
        writefln("%(%2d %)  :  %d", bits, fun(bits));
    }
}

bool xor(in bool[2] args) pure nothrow {
    return args[0] != args[1];
}

bool stu(in bool[3] args) pure nothrow {
    return args[0] || (args[1] ^ args[2]);
}

bool abcd(in bool[4] args) pure nothrow {
    return args[0] ^ (args[1] ^ (args[2] ^ args[3]));
}

void main() {
   truthTable(&xor, "A ^ B", 'A', 'B');
   truthTable(&stu, "S | ( T ^ U )", 'S', 'T', 'U');
   truthTable(&abcd, "A ^ (B ^ (C ^ D))", 'A', 'B', 'C', 'D');
}


Create a new paste based on this one


Comments: