Given the following code:
const K = a => b => a
const cat = 'cat'
const dog = 'dog'
Enter fullscreen mode
Exit fullscree...
For further actions, you may consider blocking this person and/or reporting abuse
There is no restriction to call
K
, right?Done! Both of these should output dog.
Update: You can actually put anything you want instead of cat. Or complicate it even more with infinite recursive inputs.
That violates
K(dog)()
K(K)()(dog)()
K(K(K)()(dog))(K)()
I added that rule in afterwards. These solutions were valid at the time he posted, so I'll give him credit for the creativity :)
I was the first to solve his problem :D
He changed the rules after I posted my solution.
Check the comments.
There is also a solution with a single call to
K
:)Very creative!
I assumed the other solution would have been found first.
Congratulations!
Well I updated with other solution.
haha I didn't consider
K(dog)()
would be within the rules. I have updated to rules to require bothdog
andcat
to appear exactly once ;)Lol no! You cannot change the rules after I won :P
I'm still giving you credit for both solutions ;)
In FP, there is a fairly common operation for this called
flip
. Here is a solution I tested in Chrome console.I did not consider this as a possible solution!
?
Brilliant.
Technically within the rules and as valid as any other solution!
I didn't see this one in the comments
And arrays are fun
Technically i think this follows the rules.
And if the does then this should work too
Beautiful!
So many unique solutions. Fantastic!
Well, I looked up SKI combinators. If I am understanding it correctly, and if I've implemented S correctly here...I think the following would work:
I am going to have to add "Learn SKI Combinators" to my list of things to do. :D
After you research SKI Combinators, look into Church Encoding.
These are less clever yet might be valid solutions:
lol. Clearly I have to get better at writing rules! Technically these qualify!
One more:
This one is pretty creative. Modifying the original
K
function to swapa
andb
. I like it.Hey Joel, I enjoy your articles and I think you deliver a good contribution overall to this website but I would like to see you use better use cases in your examples than cats and dogs meowing. Not for me but for others less experienced. :)
But cats and dogs are the best ;)
This call makes no sense in reality, but it will yield the desired output:
Pretty interesting solution. You are binding the first value to
this
so that thea
gets set as your second argument. Very creative!Well...
Generators and iterators. quite a unique solution. I love it!
Comma operator:
Extra hearts for the comma operator. Kudos!
Exactly! That is definitely the
I
combinator. It works beautifuly with theK
combinator in the problem to solve this problem.Two parts of
SKI
combinator calculus!Do you really need the second
dog
since you already.bind
?Maybe I do not understand, what is the second
dog
?I give up. This is still within the rules, right?
lol. not what I was looking for, but I would say it technically meets the rules.
Very creative!
Awesome work!
You found the clue I left, the K combinator! And it does come from the SKI calculus.
So many interesting solutions!