6 ms·
The Most Beautiful Piece of Code That Prints 42
- WiseWeasel 8y agoCached: https://webcache.googleusercontent.com/search?q=cache:O9K6ouDFELQJ:https://www.fluentcpp.com/2018/05/11/the-most-beautiful-piece-of-code-that-prints-42/ https://webcache.googleusercontent.com/search?q=cache:O9K6ou...
- labster 8y agoLike most code, this is way too easy in Perl 6. > perl6 -e 'say "Life, the Universe and Everything".WHY' 42
- sideshowb 8y agoFor non perl users - does this really work or are you being ironic?
- labster 8y agohttps://github.com/rakudo/rakudo/blob/master/src/core/Str.pm6#L21 https://github.com/rakudo/rakudo/blob/master/src/core/Str.pm... Oh, it works.
- sideshowb 8y agoToday I learned about meta methods. Thanks :)
- wongma 8y agoI searched for a Perl6 repl to try it on and it works! Perl is so whimsically fun. :) https://tio.run/#perl6 https://tio.run/#perl6
- aninteger 8y agoBeautiful code is simple maintainable code. In C this is a simple as: #include <stdio.h> int main(void) { puts("42"); return 0; }
- sideshowb 8y agoBeautiful code denies its own existence so perfectly you forgot you wrote it
- askvictor 8y agoI was curious why you would return a value and not just make the main function return void (in the interests of simplicity and maintainability, and assuming you're using C not C++). So I wrote my own such program, and discovered that void main(void) returns 3 to the OS. Any ideas why?
- innocenat 8y agoBecause returning void means that, on most platform, return whatever is left in %eax at the time of function termination.
- messe 8y agoI submitted it already in the linked comments section, but here it is again: #include <iostream> int main(void) { std::cout << (6 * '9') % ('h'*'2'-'g'*'2') << std::endl; }
- primitur 8y agoATDT42 (Coz 42 is Gods phone #, duh...)
- cyberferret 8y agoMAJIKTHISE: I mean what’s the use of us sitting up all night saying there may - VROOMFONDEL: Or may not be MAJIKTHISE: …or may not be… a god, if this machine comes along the next morning and gives you ‘is telephone number?
- _kst_ 8y ago#include <stdio.h> #define SIX 1+5 #define NINE 8+1 int main(void) { printf("%d * %d = %d\n", SIX, NINE, SIX * NINE); return 0; } https://github.com/Keith-S-Thompson/42 https://github.com/Keith-S-Thompson/42
- andrewflnr 8y agoI never thought code could make me feel like this.
- dynamicvariable 8y agoEver waste days debugging a subtle bug introduced by C macros? It's PTSD material. I love C but I will never understand why C has a naive macro system.
- anitil 8y agoI always thought it was a historical choice that simple substitution languages were simpler to implement so were chosen as a default. It is kind of infuriating that the preprocessor always feels simultaneously _too powerful_ and _not powerful enough_ for the jobs you want to get done. I find myself reaching for other tools (kaitai, m4 etc) a lot more these days to take the role.
- dexen 8y agoThe kenc[1] compiler subsequent to K&R C had much more constrained macro system exactly for those reasons. Thanks to advances in optimization and improved machine speed, it was reasonable to skip preprocessor-level const operations. Thanks to dead code elimination it was feasible to mostly give up conditional compilation. [1] as used, for example, in Plan 9.
- jurjenh 8y agoCould be "improved" #define SIX 1+8 #define NINE 5+1
- thezilch 8y agoNice.
- zeta0134 8y agoIt's too bad the code examples have to be in C++. When I saw the prompt, it brought to mind the fabulous Shakespeare Programming Language, which would allow for a more poetic approach: http://shakespearelang.sourceforge.net/report/shakespeare/shakespeare.html#SECTION00090000000000000000 http://shakespearelang.sourceforge.net/report/shakespeare/sh...
- RobertRoberts 8y agoHTML version: 42
- tromp 8y agoFreek Wiedijk's Most Functional winner of the 2014 International Obfuscated C Code Contest is an obvious candidate.: [1] https://www.ioccc.org/2014/wiedijk/prog.c https://www.ioccc.org/2014/wiedijk/prog.c [2] https://www.ioccc.org/2014/wiedijk/hint.html https://www.ioccc.org/2014/wiedijk/hint.html