The easiest problem you cannot solve.

JavaScript Joel - Oct 8 '18 - - Dev Community

Given the following code:

const K = a => b => a
const cat = 'cat'
const dog = 'dog'
Enter fullscreen mode Exit fullscreen mode

Calling K like this will output cat

K(cat)(dog)
//=> "cat"
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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=
Enter fullscreen mode Exit fullscreen mode

Cheers!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player