title: Thinking functionally in R with deck shuffling simulation
body: <p>I was looking at <a href="http://fivethirtyeight.com/features/can-you-deal-with-these-card-game-puzzles/" rel="nofollow noreferrer">this problem</a> (copied below), and want some help with simulating it functionally (specifically in R).</p>
  <blockquote>
  <p>On snowy afternoons, you like to play a solitaire “game” with a standard, randomly shuffled deck of 52 cards. You start dealing cards face up, one at a time, into a pile. As you deal each card, you also speak aloud, in order, the 13 card faces in a standard deck: ace, two, three, etc. (When you get to king, you start over at ace.) You keep doing this until the rank of the card you deal matches the rank you speak aloud, in which case you lose. You win if you reach the end of the deck without any matches.</p>
  <p>What is the probability that you win?</p>
  </blockquote>
  <p>Doing this via a for loop seems simple - within each loop, build the deck via <code>rep(seq(13),4)</code>, permute it, then check if any of the entries between the permuted and unpermuted deck are equal.  I'm specifically interested in a functional approach to this.  For this, I've thought to build <code>x &lt;- rep(seq(13),4)</code> once, make a 52 by <em>n</em> matrix where each column is <code>sample(x)</code>, and then find some way to check for each column if <code>sample(x) ~ x</code>, where by ~ I mean &quot;have at least one shared element, meaning the index and value are equal&quot;.  This will hopefully return true/false depending on if this is true or false.</p>
  <p>So, for <code>~</code>, if I had it called <code>compare(x,y)</code>, I would want <code>compare(c(1,2,3),c(3,1,2))</code> to be false, and <code>compare(c(1,2,3,4,5),c(8,9,10,11,5))</code> to be true.</p>
  <p>So, my questions are:</p>
  <ol>
  <li><p>What's a good (functional) way to implement compare?</p>
  </li>
  <li><p>Is there a better way to approach this problem functionally?</p>
  </li>
  </ol>