Re: To write such an expressive code D
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. :)
Re: To write such an expressive code D
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.
Re: To write such an expressive code D
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
Re: To write such an expressive code D
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).
Re: To write such an expressive code D
On Tue, 10 Feb 2015 11:33:54 +, bearophile wrote: > Dennis Ritchie: > >> Please help. > > This starts to look like homework :-) it's much worse: meaningless pseudocomparison of different languages for nothing. signature.asc Description: PGP signature
Re: To write such an expressive code D
On Tuesday, 10 February 2015 at 11:41:20 UTC, ketmar wrote: On Tue, 10 Feb 2015 11:33:54 +, bearophile wrote: Dennis Ritchie: Please help. This starts to look like homework :-) it's much worse: meaningless pseudocomparison of different languages for nothing. This task can be solved for D?
Re: To write such an expressive code D
On Tuesday, 10 February 2015 at 11:33:54 UTC, bearophile wrote: Dennis Ritchie: Please help. This starts to look like homework :-) Bye, bearophile This is not homework - this is a war of code on C#/F# and D. I've been programming in D, my opponent on F#/C#.
Re: To write such an expressive code D
Dennis Ritchie: Please help. This starts to look like homework :-) Bye, bearophile
Re: To write such an expressive code D
Please help. import std.stdio; import std.stdio; void main() { /* return (a xor b xor c) */ int nobitxor(int a, int b, int c) { return (a + b + c == 2 || a + b + c == 0) ? 0 : 1; } int a, b, c; a = b = c = 0; foreach (i; 0 .. 8) { if (i > 3) a = 1; if (i == 2 || i == 3 || i == 6 || i == 7) b = 1; if (i % 2) c = 1; writeln(a, b, c, ' ', nobitxor(a, b, c)); a = b = c = 0; } } Output: 000 0 001 1 010 1 011 0 100 1 101 0 110 0 111 1 You need to function nobitxor(int a, int b, int c) not used bitwise/logical and mathematical operations.
Re: To write such an expressive code D
On Tue, 10 Feb 2015 08:40:36 +, Dennis Ritchie wrote: > On Tuesday, 10 February 2015 at 08:12:00 UTC, Vladimir Panteleev wrote: >> Why is that? > > ÐоÑÐ¾Ð¼Ñ ÑÑо Ñ ÑпоÑил Ñ Ð¾Ð´Ð½Ð¸Ð¼ ÑпÑÑÑÑм > Ñеловеком, коÑоÑÐ¾Ð¼Ñ Ð½Ðµ нÑавиÑÑÑ D, > на ÑÑом ÑоÑÑме: > http://www.cyberforum.ru/holywars/thread1367892-page13.html Ðн пÑоÑил > Ð¼ÐµÐ½Ñ Ð½Ð°Ð¿Ð¸ÑаÑÑ ÑакÑÑ Ð¿ÑогÑÐ°Ð¼Ð¼Ñ Ñ > иÑполÑзованием ÑолÑко возможноÑÑей ÑзÑка > и ÑÑнкÑии sin(). > > Because I was arguing with one quiet a stubborn person who does not like > D, on this forum: > http://www.cyberforum.ru/holywars/thread1367892-page13.html He asked me > to write such a program using only the language features and functions > sin(). 'cause he is a dumb asshead, that's it. signature.asc Description: PGP signature
Re: To write such an expressive code D
On Tuesday, 10 February 2015 at 08:40:38 UTC, Dennis Ritchie wrote: Because I was arguing with one quiet a stubborn person who does not like D, on this forum: http://www.cyberforum.ru/holywars/thread1367892-page13.html He asked me to write such a program using only the language features and functions sin(). If someone makes stupid demands like this one to justify his dislike for the language, such person is either deliberate troll or has strong enough prejudice no never like language anyway, arguments or not. Language features don't magically appear from nowhere - those come at cost of extra code in compiler and/or runtime library making it very hard to use language with smaller runtime (D is actually guilty of that). It is a common practice to treat standard language library as part of language. Both C and C++ include detailed spec on standard library in official language spec for example. As such making any distinction between two is impractical.
Re: To write such an expressive code D
On Tuesday, 10 February 2015 at 08:40:38 UTC, Dennis Ritchie wrote: On Tuesday, 10 February 2015 at 08:12:00 UTC, Vladimir Panteleev wrote: Why is that? Потому что я спорил с одним упёртым человеком, которому не нравится D, на этом форуме: http://www.cyberforum.ru/holywars/thread1367892-page13.html Он просил меня написать такую программу с использованием только возможностей языка и функции sin(). Because I was arguing with one quiet a stubborn person who does not like D, on this forum: http://www.cyberforum.ru/holywars/thread1367892-page13.html He asked me to write such a program using only the language features and functions sin(). How to win the holy language war: 1. Pick a feature that only one of the languages has 2. Pick a task that this feature solves neatly 3. Solve it using that feature 4. Forbid every other solution not involving the features that only your preferred language has. Done.
Re: To write such an expressive code D
On Tuesday, 10 February 2015 at 08:12:00 UTC, Vladimir Panteleev wrote: Why is that? Потому что я спорил с одним упёртым человеком, которому не нравится D, на этом форуме: http://www.cyberforum.ru/holywars/thread1367892-page13.html Он просил меня написать такую программу с использованием только возможностей языка и функции sin(). Because I was arguing with one quiet a stubborn person who does not like D, on this forum: http://www.cyberforum.ru/holywars/thread1367892-page13.html He asked me to write such a program using only the language features and functions sin().
Re: To write such an expressive code D
On Tuesday, 10 February 2015 at 04:17:48 UTC, Dennis Ritchie wrote: I just need that code was only used features of the language without using library functions. You may only use the function sin(). Why is that? Although D has a lot of language features, D tries to push functionality into the library as often as possible. This is better than having language features for everything, because you can then reimplement, tweak or replace said features by simply writing D code.
Re: To write such an expressive code D
On Tuesday, 10 February 2015 at 06:17:17 UTC, Ali Çehreli wrote: On 02/09/2015 08:17 PM, Dennis Ritchie wrote: > Ali, and you can write it without using the function "iota()" and map? No because the a..b syntax is not a D language construct that we can use anywhere that it makes sense. It only works as number ranges inside foreach loops, when indexing slices, and case value ranges. Also no, because there is no equivalent of F#'s -> syntax, we have to use map. > "%(%.15g\n%)".writefln(iota(0, PI/2, PI/2/9).map!sin); > > I just need that code was only used features of the language without > using library functions. You may only use the function sin(). I am waiting to see a language solution that does not use even sin(). :o) Ali Thank you.
Re: To write such an expressive code D
On 02/09/2015 08:17 PM, Dennis Ritchie wrote: > Ali, and you can write it without using the function "iota()" and map? No because the a..b syntax is not a D language construct that we can use anywhere that it makes sense. It only works as number ranges inside foreach loops, when indexing slices, and case value ranges. Also no, because there is no equivalent of F#'s -> syntax, we have to use map. > "%(%.15g\n%)".writefln(iota(0, PI/2, PI/2/9).map!sin); > > I just need that code was only used features of the language without > using library functions. You may only use the function sin(). I am waiting to see a language solution that does not use even sin(). :o) Ali
Re: To write such an expressive code D
On Monday, 9 February 2015 at 20:16:45 UTC, Ali Çehreli wrote: Yes, but apparently D's default precision for output is less than F#'s so how about the following? :p "%(%.15g\n%)".writefln(iota(0, PI/2, PI/2/9).map!sin); Just for demonstration, I would not write anything like that but the following is fine because now the format becomes the second parameter: :) iota(0, PI/2, PI/2/9).map!sin.writeF("%(%.15g\n%)"); void writeF(R)(R range, string format) { return writefln(format, range); } Ali, and you can write it without using the function "iota()" and map? "%(%.15g\n%)".writefln(iota(0, PI/2, PI/2/9).map!sin); I just need that code was only used features of the language without using library functions. You may only use the function sin().
Re: To write such an expressive code D
On 02/09/2015 12:05 PM, Dennis Ritchie wrote: On Monday, 9 February 2015 at 20:03:00 UTC, Vladimir Panteleev wrote: On Monday, 9 February 2015 at 19:57:23 UTC, Ali Çehreli wrote: writefln("%(%.15g\n%)", sins); In 2.067, you can write: iota(0, PI/2, PI/2/9).map!sin.each!writeln; March 1! Yes, but apparently D's default precision for output is less than F#'s so how about the following? :p "%(%.15g\n%)".writefln(iota(0, PI/2, PI/2/9).map!sin); Just for demonstration, I would not write anything like that but the following is fine because now the format becomes the second parameter: :) iota(0, PI/2, PI/2/9).map!sin.writeF("%(%.15g\n%)"); void writeF(R)(R range, string format) { return writefln(format, range); } Ali
Re: To write such an expressive code D
On Monday, 9 February 2015 at 20:03:00 UTC, Vladimir Panteleev wrote: On Monday, 9 February 2015 at 19:57:23 UTC, Ali Çehreli wrote: writefln("%(%.15g\n%)", sins); In 2.067, you can write: iota(0, PI/2, PI/2/9).map!sin.each!writeln; March 1!
Re: To write such an expressive code D
On Monday, 9 February 2015 at 19:57:23 UTC, Ali Çehreli wrote: writefln("%(%.15g\n%)", sins); In 2.067, you can write: iota(0, PI/2, PI/2/9).map!sin.each!writeln;
Re: To write such an expressive code D
Thank you, Tobias Pankrath and Ali Çehreli.
Re: To write such an expressive code D
On 02/09/2015 11:45 AM, Tobias Pankrath wrote: iota(0, 91, 10).map!sin.writeln or something like that. Yes: :) import std.math; import std.stdio; import std.range; import std.algorithm; void main() { const beg = 0.0L; const interval = PI_2 / 9; const end = PI_2 + interval; auto sins = iota(beg, end, interval).map!sin; writefln("%(%.15g\n%)", sins); } 0 0.17364817766693 0.342020143325669 0.5 0.642787609686539 0.76603118978 0.866025403784439 0.939692620785908 0.984807753012208 1 Ali
Re: To write such an expressive code D
On Monday, 9 February 2015 at 19:40:42 UTC, Dennis Ritchie wrote: Good evening. Is it possible to D something to replace the container on the F#, which displays the values of the sine from 0 to 90 degrees with an interval of 10 degrees: let pi = Math.PI let sins = [for x in 0.0..pi / 2.0 / 9.0..pi / 2.0 -> sin x] sins.Dump() Output: 0 0,17364817766693 0,342020143325699 0,5 0,642787609686539 0,76603118978 0,866025403784439 0,939692620785908 0,984807753012208 1 P.S. Interested in code that will be as impressive as this. In General, I would like to see something akin to D. iota(0, 91, 10).map!sin.writeln or something like that.
To write such an expressive code D
Good evening. Is it possible to D something to replace the container on the F#, which displays the values of the sine from 0 to 90 degrees with an interval of 10 degrees: let pi = Math.PI let sins = [for x in 0.0..pi / 2.0 / 9.0..pi / 2.0 -> sin x] sins.Dump() Output: 0 0,17364817766693 0,342020143325699 0,5 0,642787609686539 0,76603118978 0,866025403784439 0,939692620785908 0,984807753012208 1 P.S. Interested in code that will be as impressive as this. In General, I would like to see something akin to D.