Help us understand the problem. What is going on with this article?

# 元記事

ヌジョレーボーボー @mattn

mattnさん

```[
["ボ", ""],
["ジョ", ""],
["レ", "ー"],
["ヌ", "ー"],
["ボ", "ー"]
]
```

この 1 element 目だけを shuffle して全体 flatten みたいなのが綺麗に書ける言語は強そうですね。

これについて自分なりに書いてみた

```module BeaujolaisNouveau where

import Control.Arrow
import System.Random

main :: IO ()
main = do
let beaujolaisNouveau = [ ["ボ", ""]
, ["ジョ", ""]
, ["レ", "ー"]
, ["ヌ", "ー"]
, ["ボ", "ー"]
]
res <- (liftM concat \$ liftM concat \$ shuffle1st beaujolaisNouveau)
putStrLn res

shuffle1st :: [[a]] -> IO [[a]]
shuffle1st xs = do
xs' <- shuffle \$ fstL xs
return \$ zipWith (\x y -> [x, y]) xs' (sndL xs)

shuffle :: [a] -> IO [a]
shuffle (x:[])= return [x]
shuffle xs = do
pickedIndex <- randomRIO (0, (length xs) - 1)
let picked = xs !! pickedIndex

rest <- shuffle \$ restOf (pickedIndex + 1) xs
return \$ picked : rest

restOf :: Int -> [a] -> [a]
restOf _ (x:[]) = []
restOf n xs     = take (n - 1) xs ++ (drop n xs)

fstL :: [[a]] -> [a]
fstL []     = []
fstL (x:xs) = head x : fstL xs

sndL :: [[a]] -> [a]
sndL []     = []
sndL (x:xs) = head (tail x) : sndL xs
```

# 結果

[2019-11-27 17:43]

Why do not you register as a user and use Qiita more conveniently?
1. We will deliver articles that match you
By following users and tags, you can catch up information on technical fields that you are interested in as a whole
2. you can read useful information later efficiently
By "stocking" the articles you like, you can search right away