Given the following code:
const K = a => b => a
const cat = 'cat'
const dog = 'dog'
Calling K
like this will output cat
K(cat)(dog)
//=> "cat"
How can you call K
to output dog
without swapping the order of cat
and dog
?
cat
and dog
must appear exactly once.
The Given cannot be modified.
// INVALID: Cannot swap order!
K(dog)(cat)
// NO CHEATING: May only appear once.
K.bind(null, dog)(cat)(dog)
I know of two ways to solve this problem. Surprise me with another!
I should have also specified no bind, call, or apply, but the solutions are just too interesting :)
Hints: K
in the problem is the K combinator, part of the SKI combinator calculus.
Spoilers are in the comments! BEWARE!
Many great solutions have been posted below! A lot I never considered. Also some very creative loopholes around the rules I created ;)
Here's one solution that went undiscovered. It's base64 encoded to hide the "spoiler". But if you are curious you can decode it using your console's atob
function.
Syh4PT54KShjYXQpKGRvZyk=