r/dailyprogrammer • • May 22 '15
[2015-05-22] Challenge #215 [Hard] Metaprogramming Madness!
Description
You're working in the devils language. Looser than PHP, more forgiving than Javascript, and more infuriating than LOLCODE.
You've had it up to here with this language (and you're a tall guy) so you sit down and think of a solution and then all of a sudden it smacks you straight in the face. Figuratively.
Your comparisons are all over the place since you can't really tell what types evaluate to True and what types evaluate to False. It is in this slightly worrying and dehydrated state that you declare you'll output a truth table for that language in the language!
Armed with a paper cup of saltwater and a lovely straw hat, you set about the task! Metaprogramming ain't easy but you're not phased, you're a programmer armed with nimble fingers and a spongy brain. You sit down and start typing, type type type
...Oh did I mention you're on an island? Yeah there's that too...
Formal Inputs & Outputs
Given a programming language, output its corresponding truth table. Only the most basic of types need to be included (If you're in a language that doesn't have any of these types, ignore them).
- Int
- Float
- Char
- String
- Array
- Boolean
Input description
N/A
Output description
A truth table for the language that you're programming in.
e.g.
| Expression | Bool |
|---|---|
| "Hello World!" | True |
| '' | False |
| '0' | True |
| 1 | True |
| 0 | False |
| 0.0 | False |
| [] | False |
| [1,2,3] | True |
| True | True |
| False | False |
Finally
Have a good challenge idea?
Consider submitting it to /r/dailyprogrammer_ideas
35
u/prophile May 22 '15
Haskell:
16
u/marchelzo May 22 '15
Brainfuck:
8
u/wadehn May 22 '15 edited May 22 '15
C++ with a bit of template metaprogramming. Unfortunately, C++ still has the same implicit conversions as C:
Output:
7
u/curtmack May 22 '15 edited May 22 '15
Clojure
Clojure has a really liberal definition of truth. Everything is true except
falseandnil!Output:
Edit: Added keywords.
5
u/zeringus May 22 '15
Ruby:
Output:
4
u/weekendblues May 22 '15
x86_64 Assembly:
Output:
This challenge was particularly easy to write in assembly, since not only does assembly not evaluate basic types as Boolean in certain situations, it actually doesn't have any basic types at all (unless you consider raw data of various sizes (byte, word, double word, and quad word) to be "types"; the processor certainly doesn't). I suppose one could make the case that floating point numbers are different, but they're really just strings of bits that are in ieee 754 format. In any event, as an optimization I elected not to bother printing a header since there wasn't anything to put in the table, so the program just returns 0. Runs lightning fast.
All joking aside, I have to say I'm a little bit disappointed this is being labeled as a "hard" challenge. While it's a fun exercise in some languages, it doesn't seem remotely as difficult as this weeks easy or intermediate challenges, let alone last week's hard challenge. But that's just my opinion; difficultly is, after all, somewhat subjective.
3
u/JakDrako May 22 '15
VB.Net
I pass the expressions as string to a function that compiles and runs them, returning the result or the error message (either compile time or runtime). Interesting challenge for a compiled language.
Results
Hmmm... for some reason the error messages are sometimes in French. Nice.
3
u/dangerbird2 May 22 '15
Scheme
3
u/codeman869 May 23 '15
Ruby I hope this fits the bill, decided to put the class before the value to make it easier to understand.
Output:
2
u/binaryblade May 22 '15
go, making some horrible use of interface and reflect, there may be some magic boolean conversion foo in the stdlib I don't know about. But all things considered I don't think I want to know because then I might be tempted to use them and that would be unfortunate.
2
u/franza73 May 22 '15 edited May 23 '15
Perl solution.
Resulting table (types in perl are 'scalar', 'array' and 'hash'):
2
u/Vectorious May 22 '15 edited Sep 18 '25
toy scale roof money thumb angle toothbrush hard-to-find spectacular sophisticated
This post was mass deleted and anonymized with Redact
2
u/chipaca May 22 '15 edited May 22 '15
Python 3.
outputs:
edit: same program, with python 2.7, outputs
2
u/robin-gvx 0 2 May 22 '15
Isle:
Output:
Only
nilis falsy.Fun fact: the
!operator is a bit more liberal with taking values as falsy: it basically uses Python semantics, so!0and!""are truthy. The exception is for tables, which uses the'!'method, so!('!'=do 17 end) == 171
u/glenbolake 2 0 May 22 '15 edited May 22 '15
Python 2.7. I wanted to do some trickery with "import types" and lots of eval() calls, but I had a lot of trouble trying to generate constructors.
EDIT: Here's my attempt at automating it. Some stuff gets reported twice, because types has both DictType and DictionaryType (for example)
Automated output:
1
u/Godspiral 3 3 May 22 '15 edited May 22 '15
In J, understanding the challenge similarly to zeringus
3rd column is a type code (1 is boolean, 4 is integer). 4th column is scalar, list (table, brick for higher dimensions)
unusual demonness for the if. function, any data that starts with 0 is false (including long arrays). Empty/null data is true. 0.0 is parsed away to 0 before you can look at it.
but the if. function is not the only use for truth tables, and this tacit function behaves by skipping nulls, and evaluating each scalar value for truth)
the blank boxes have null as the truth table entry, and that has the convenience that further tacit functions will also treat it as null and skip evaluation. The truth tables for each expression are in the same shape as the original data.
results for 2 times false, true, and null:
1
u/__MadHatter May 22 '15 edited May 23 '15
Java. Constructive criticism/tips welcome. Edit: updated source as per Godspiral's question. I was not able to convert anything but string variables to boolean. Hence, the overridden comparisons.
Output:
1
u/G33kDude 1 1 May 22 '15
I'm not sure I understand the exercise, but here goes. In AutoHotkey typing is very loose. However, strings and variables containing strings are treated differently. Because of that, I can't really use a loop. Also,
:=is assignment.Output: http://i.imgur.com/L3SV6n2.png
1
u/VikingofRock May 22 '15 edited May 22 '15
C++ (because as /u/prophile pointed out, this is really boring in Haskell):
Output:
I replaced the arrays in the OP with null and non-null pointers, because those seemed more fundamental to C++ to me.
1
u/cvpcs May 22 '15
C#
I set up my code to continuously allow the user to type input. That input then is passed to Microsoft's CSharpCodeProvider and compiled into an evaluation class that I can call to get back the object representation of the evaluated line. If the compile succeeds then it is run and the object is passed to a type converter to try to convert it to a Boolean.
Code:
Output:
1
u/pbeard_t 0 1 May 22 '15 edited May 22 '15
C99. Edit: cleanup and added a couple more.
Output
1
u/r0naa May 23 '15
Python 3.4
1
u/kirsybuu 0 1 May 23 '15
D Language:
Output:
Quirks: An array is "true" if either pointer or length are true, and an associative array is "true" if its pointer is true regardless of length, so you can't use this test to reliably check for emptiness. The typeid.init example even shows a null array with nonzero length!
1
u/Tipaa May 23 '15
D:
Output:
1
u/[deleted] May 23 '15
I'm not sure I understand the challenge, but looking at what other people have done I guess this is correct (python 3):
1
u/mikevdg May 24 '15 edited May 24 '15
I ahh... what!?
Smalltalk, VisualWorks v7.9:
The only things which can be true or false are members of Boolean, which in the default configuration has only members
trueandfalse. If you try to use anything else like a Boolean, you getUnhandled exception: NonBoolean receiver -- proceed for truth.which I find quite zen.If you wanted to, you could create more Boolean and call it whatever you want (i.e. 'true', 'false' and 'bob'). However, VW said
You may not create any more Booleans - this is two-valued logic. Again, pretty zen, huh!? You'd have to hack past the error message which would take a minute.I am not going to catch the exception that occurs when using a non-boolean and pretend the result is false. That is such a bad code smell that I vomit in it's general direction. If you force me to do that, I'd quit and you'd have to hire some hapless student to write code that disgusting.
So:
Output:
Fun fact: somebody once swapped true and false around (in code,
true become: false). This physically swaps the objects in memory; true is now false and false is now true. The system kept running and didn't crash!1
u/astralwanderer May 27 '15
Ruby version, with monkey-patching
1
u/[deleted] Jun 03 '15
javascript, anyone?
1
u/Tetsumi- 1 0 Jun 30 '15
Language: CHICKEN (Scheme)
Output:
1
u/NotoriousArab Aug 06 '15 edited Aug 07 '15
Simple JavaScript solution:
1
u/[deleted] Sep 10 '15 edited Sep 10 '15
Booleans in Fortran are called "Logical" variables. There is no "standard" way to convert integer and other variable types to Logical, in other words the outcome is compiler-dependant.
So, it is non-portable to do, Integer n If (n) ...
Instead you are supposed to do "If (n/=0) ..."
That said, there are three ways we can implicitly convert. One, we can use a formatted write statement (or print) and use a Logical edit descriptor for the variable.
Two, we can use the Transfer function which does a byte-level transfer of the variable into a Logical data type.
Three, we can plug the variable into an IF(n) statement, directly evaluating it as a logical value.
I would not expect this to even compile on a different system or compiler. There was a lot of trial and error to get this to run!
On Intel fortran, only logicals and integers are allowed in a logical expression.
It looks like ifort is consistent between methods when it allows them to occur. Note that the integers alternate T/F/T/F - I think this means the LSB is the only one tested for truth. Same phenomenon for character strings - it looks like only the first character is converted to integer, then the LSB of that is tested for truth.
drumroll....
1
u/valdogg21 May 22 '15
Python 2.7
Input:
Output:
4
u/[deleted] May 22 '15 edited May 23 '15
[removed] — view removed comment