Help for Patience in Scheme. This will deal out the initial piles, based on the order of cards in 'sample': > (deal (sample) 'k) ((a (k c) (5 d) (9 c) (3 c)) (2 (k s) (4 d) (6 s) (6 c)) (3 (t h) (a d) (4 c) (q d)) (4 (2 h) (5 h) (3 s) (t c)) (5 (9 h) (4 h) (8 c) (5 c)) (6 (k h) (2 c) (3 d) (a c)) (7 (3 h) (t d) (8 h) (7 d)) (8 (2 d) (q s) (j s) (j c)) (9 (q h) (2 s) (7 s) (k d)) (t (8 d) (j d) (5 s) (6 h)) (j (8 s) (7 h) (9 s) (a h)) (q (q c) (j h) (4 s) (7 c)) (k (t s) (9 d) (6 d) (a s))) > (define piles (deal sample 'k)) > piles ((a (k c) (5 d) (9 c) (3 c)) (2 (k s) (4 d) (6 s) (6 c)) (3 (t h) (a d) (4 c) (q d)) (4 (2 h) (5 h) (3 s) (t c)) (5 (9 h) (4 h) (8 c) (5 c)) (6 (k h) (2 c) (3 d) (a c)) (7 (3 h) (t d) (8 h) (7 d)) (8 (2 d) (q s) (j s) (j c)) (9 (q h) (2 s) (7 s) (k d)) (t (8 d) (j d) (5 s) (6 h)) (j (8 s) (7 h) (9 s) (a h)) (q (q c) (j h) (4 s) (7 c)) (k (t s) (9 d) (6 d) (a s))) This picks out one pile. It uses a predefined 'assoc' function that associates a key with a list of lists. > (assoc 'j piles) (j (8 s) (7 h) (9 s) (a h)) This uses 'set-cdr!' to remove a card from the pile with Jack as the key value: > (set-cdr! (assoc 'j piles) (cddr (assoc 'j piles))) > piles ((a (k c) (5 d) (9 c) (3 c)) (2 (k s) (4 d) (6 s) (6 c)) (3 (t h) (a d) (4 c) (q d)) (4 (2 h) (5 h) (3 s) (t c)) (5 (9 h) (4 h) (8 c) (5 c)) (6 (k h) (2 c) (3 d) (a c)) (7 (3 h) (t d) (8 h) (7 d)) (8 (2 d) (q s) (j s) (j c)) (9 (q h) (2 s) (7 s) (k d)) (t (8 d) (j d) (5 s) (6 h)) (j (7 h) (9 s) (a h)) (q (q c) (j h) (4 s) (7 c)) (k (t s) (9 d) (6 d) (a s))) The card removed was (8 s). The 'nextrank' will be 8. So next you'll need to remove the (2 d) card off of pile 8. The 'nextrank' will then be k from the (k s). Keep going until the pile corresponding to 'nextrank' has no more cards. Here's an example snapshot of the piles the end of one game. The last card was (k d). The nextrank is 'k', but there are no more cards on the 'k' pile. ((a (3 c)) (2) (3 (q d)) (4) (5 (5 c)) (6) (7 (8 h) (7 d)) (8 (j c)) (9) (t) (j (a h)) (q (7 c)) (k)) >