February 10, 2015 Re: To write such an expressive code D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | F#: let f = function | 0 , 0 , 0 -> 0 | 0 , 1 , 1 -> 0 | 1 , 0 , 1 -> 0 | 1 , 1 , 0 -> 0 | _ -> 1 for a in 0..1 do for b in 0..1 do for c in 0..1 do printfn "%i xor %i xor %i = %i" a b c (f (a, b, c)) Output: 0 xor 0 xor 0 = 0 0 xor 0 xor 1 = 1 0 xor 1 xor 0 = 1 0 xor 1 xor 1 = 0 1 xor 0 xor 0 = 1 1 xor 0 xor 1 = 0 1 xor 1 xor 0 = 0 1 xor 1 xor 1 = 1 This man again took advantage of the fact that in D there is no such operation -> (analog switch). |
February 11, 2015 Re: To write such an expressive code D | ||||
---|---|---|---|---|
| ||||
Posted in reply to Dennis Ritchie | Dennis Ritchie:
> Output:
>
> 0 xor 0 xor 0 = 0
> 0 xor 0 xor 1 = 1
> 0 xor 1 xor 0 = 1
> 0 xor 1 xor 1 = 0
> 1 xor 0 xor 0 = 1
> 1 xor 0 xor 1 = 0
> 1 xor 1 xor 0 = 0
> 1 xor 1 xor 1 = 1
>
> This man again took advantage of the fact that in D there is no such operation -> (analog switch).
A natural solution in D:
void main() {
import std.stdio;
foreach (immutable a; 0 .. 2)
foreach (immutable b; 0 .. 2)
foreach (immutable c; 0 .. 2)
writefln("%d xor %d xor %d = %d", a, b, c, (a + b + c) % 2);
}
Alternative solution closer to the F# code:
import std.stdio, std.algorithm, std.typecons;
int f(T)(T t) if (isTuple!T) {
return t.predSwitch(
tuple(0, 0, 0), 0,
tuple(0, 1, 1), 0,
tuple(1, 0, 1), 0,
tuple(1, 1, 0), 0,
/*else*/ 1);
}
void main() {
foreach (immutable a; 0 .. 2)
foreach (immutable b; 0 .. 2)
foreach (immutable c; 0 .. 2)
writefln("%d xor %d xor %d = %d", a, b, c, tuple(a, b, c).f);
}
Bye,
bearophile
|
February 11, 2015 Re: To write such an expressive code D | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On Wednesday, 11 February 2015 at 00:56:03 UTC, bearophile wrote:
> Dennis Ritchie:
>
>> Output:
>>
>> 0 xor 0 xor 0 = 0
>> 0 xor 0 xor 1 = 1
>> 0 xor 1 xor 0 = 1
>> 0 xor 1 xor 1 = 0
>> 1 xor 0 xor 0 = 1
>> 1 xor 0 xor 1 = 0
>> 1 xor 1 xor 0 = 0
>> 1 xor 1 xor 1 = 1
>>
>> This man again took advantage of the fact that in D there is no such operation -> (analog switch).
>
> A natural solution in D:
>
> void main() {
> import std.stdio;
>
> foreach (immutable a; 0 .. 2)
> foreach (immutable b; 0 .. 2)
> foreach (immutable c; 0 .. 2)
> writefln("%d xor %d xor %d = %d", a, b, c, (a + b + c) % 2);
> }
>
>
>
> Alternative solution closer to the F# code:
>
> import std.stdio, std.algorithm, std.typecons;
>
> int f(T)(T t) if (isTuple!T) {
> return t.predSwitch(
> tuple(0, 0, 0), 0,
> tuple(0, 1, 1), 0,
> tuple(1, 0, 1), 0,
> tuple(1, 1, 0), 0,
> /*else*/ 1);
> }
>
> void main() {
> foreach (immutable a; 0 .. 2)
> foreach (immutable b; 0 .. 2)
> foreach (immutable c; 0 .. 2)
> writefln("%d xor %d xor %d = %d", a, b, c, tuple(a, b, c).f);
> }
>
> Bye,
> bearophile
Thanks.
|
February 11, 2015 Re: To write such an expressive code D | ||||
---|---|---|---|---|
| ||||
Posted in reply to bearophile | On 2015-02-11 at 01:56, bearophile wrote: > Alternative solution closer to the F# code: > > import std.stdio, std.algorithm, std.typecons; > > int f(T)(T t) if (isTuple!T) { > return t.predSwitch( > tuple(0, 0, 0), 0, > tuple(0, 1, 1), 0, > tuple(1, 0, 1), 0, > tuple(1, 1, 0), 0, > /*else*/ 1); > } > > void main() { > foreach (immutable a; 0 .. 2) > foreach (immutable b; 0 .. 2) > foreach (immutable c; 0 .. 2) > writefln("%d xor %d xor %d = %d", a, b, c, tuple(a, b, c).f); > } Why bend over and try to make it F#? Screw the F# guy. He was cheating with a switch, so why can't we cheat? foreach(i;0..8)writefln("%d xor %d xor %d = %s",!!(i&4),!!(i&2),!!(i&1),"01101001"[i]); Assimilate this! Oh wait, you needed a function. OK, here's a function (and just replace "01101001"[i] with xxor(i&4,i&2,i&1)): int xxor(int a, int b, int c) {return (a&&b&&c)||(!a&&!b&&c)||(!a&&b&&!c)||(a&&!b&&!c);} If it makes him dislike D even more, great! Mission accomplished. :) |
Copyright © 1999-2021 by the D Language Foundation