Mind boggling card trick: Difference between revisions
Content added Content deleted
m (→{{header|zkl}}: tweak comment) |
(→{{header|Haskell}}: (Some tidying of the threeStack function, refreshed sample output)) |
||
Line 290: | Line 290: | ||
] |
] |
||
return ns |
return ns |
||
-- RED vs BLACK ---------------------------------------- |
-- RED vs BLACK ---------------------------------------- |
||
rb :: Int -> Char |
rb :: Int -> Char |
||
Line 299: | Line 300: | ||
-- THREE STACKS ---------------------------------------- |
-- THREE STACKS ---------------------------------------- |
||
threeStacks :: String -> (String, String, String) |
threeStacks :: String -> (String, String, String) |
||
threeStacks |
threeStacks = go ([], [], []) |
||
where |
where |
||
go tpl [] = tpl |
go tpl [] = tpl |
||
go (rs, bs, ds) |
go (rs, bs, ds) [x] = (rs, bs, x : ds) |
||
go (rs, bs, ds) (x: |
go (rs, bs, ds) (x:y:xs) |
||
| 'R' == x = go (y:rs, bs, x:ds) xs |
| 'R' == x = go (y : rs, bs, x : ds) xs |
||
| otherwise = go (rs, y:bs, x:ds) xs |
| otherwise = go (rs, y : bs, x : ds) xs |
||
-- SHUFFLE ----------------------------------------------- |
-- SHUFFLE ----------------------------------------------- |
||
-- (See Knuth Shuffle task) |
-- (See Knuth Shuffle task) |
||
knuthShuffle :: [a] -> IO [a] |
knuthShuffle :: [a] -> IO [a] |
||
knuthShuffle xs = (foldr swapElems xs . zip [1 ..]) <$> randoms (length xs) |
knuthShuffle xs = (foldr swapElems xs . zip [1 ..]) <$> randoms (length xs) |
||
Line 326: | Line 326: | ||
in a ++ c : drop 1 b</lang> |
in a ++ c : drop 1 b</lang> |
||
{{Out}} |
{{Out}} |
||
<pre>Discarded: |
<pre>Discarded: RRRRRBBBBBRBBBRBBRBRRBRRRB |
||
Swapped: |
Swapped: 7 |
||
Red pile: |
Red pile: BBBRRBRBRBBRR |
||
Black pile: |
Black pile: BRRRRRBBRBBRB |
||
RRRRRR = Red cards in the red pile |
|||
BBBBBB = Black cards in the black pile |
|||
True</pre> |
True</pre> |
||