Mind boggling card trick: Difference between revisions

Content added Content deleted
m (→‎{{header|Javascript}}: minor tidying)
(→‎{{header|Haskell}}: Wrapped the swap of n cards in an exchange function)
Line 273: Line 273:
-- SWAPPED
-- SWAPPED
nSwap <- randomRIO (1, min (length rs_) (length bs_))
nSwap <- randomRIO (1, min (length rs_) (length bs_))
let bs = take nSwap rs_ <> drop nSwap bs_
let (rs, bs) = exchange nSwap rs_ bs_
let rs = take nSwap bs_ <> drop nSwap rs_
-- CHECKED
-- CHECKED
Line 290: Line 289:
]
]
return ns
return ns



-- RED vs BLACK ----------------------------------------
-- RED vs BLACK ----------------------------------------
Line 307: Line 305:
| '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

exchange :: Int -> [a] -> [a] -> ([a], [a])
exchange n xs ys =
let [xs_, ys_] = splitAt n <$> [xs, ys]
in (fst ys_ <> snd xs_, fst xs_ <> snd ys_)


-- SHUFFLE -----------------------------------------------
-- SHUFFLE -----------------------------------------------