memo

2010-12-07

mapM (const ['0'..'9']) [1..3] がよく分からない

展開してみれば分かるはず。

mapM (const ['0'..'9']) [1..3] -- -> ['000', '001', '002', ..., '998', '999]

sequence [['0'..'9'], ['0'..'9'], ['0'..'9']]

do { x <- ['0'..'9']; xs <- do { x' <- ['0'..'9']; xs' <- do { x'' <- ['0'..'9']; xs'' <- return []; return (x'':xs'') }; return (x':xs') }; return (x:xs) }
{-
do { x <- ['0'..'9'];
     xs <- do { x' <- ['0'..'9'];
                xs' <- do { x'' <- ['0'..'9'];
                            xs'' <- return [];
                            return (x'':xs'') };
                return (x':xs') };
     return (x:xs) }
-}

['0'..'9'] >>= \x -> (['0'..'9'] >>= \x' -> (['0'..'9'] >>= \x'' -> [[]] >>= \xs'' -> [x'':xs'']) >>= \xs' -> [x':xs']) >>= \xs -> [x:xs]

{-
['0'..'9'] >>= \x -> (['0'..'9'] >>= \x' -> (concatMap (\xs'' -> concatMap (\x'' -> [x'':xs'']) ['0'..'9']) [[]]) >>= \xs' -> [x':xs']) >>= \xs -> [x:xs]
-}
concatMap (\xs -> concatMap (\x -> [x:xs]) ['0'..'9']) (concatMap (\xs' -> concatMap (\x' -> [x':xs']) ['0'..'9']) (concatMap (\xs'' -> concatMap (\x'' -> [x'':xs'']) ['0'..'9']) [[]]))

と期待したけど、そうでもなかった。

sequence の定義は ここ を参考に。