4 ms·
I find it a bit hard to see how to reinterpret your Python code in the list monad. Can you post a Haskell version of this function?
by longlivedeath 15y ago
I find it a bit hard to see how to reinterpret your Python code in the list monad. Can you post a Haskell version of this function?
- psykotic 15y agoHey, sorry for the late reply. It seems pretty obvious to me, so I'm not sure what is confusing you. class Monad m => ChoiceMonad m where choice :: [a] -> m a For the list monad, choice is just the identity function. For the probability monad, choice picks a random element from the list. (Ideally choice would take a random access structure like a set as its argument, but I'm doing it the simplest way here.) To implement shuffle, you more or less just transliterate the Python code as you'd expect. Before you tackle this, make you sure completely understand how the same situation plays out for the simpler case of subsets: subset :: ChoiceMonad m => [a] -> m [a] subset [] = return [] subset (x:xs) = do b <- choice [True, False] xs' <- subset xs return (if b then x:xs' else xs') Over the list monad, this generates a list of all subsets. Over the probability monad, it generates a random subset.
- longlivedeath 15y agoThank you, now I see. Here's my version: shuffle :: (ChoiceMonad m, Eq a) => [a] -> m [a] shuffle [] = return [] shuffle elems = do x <- choice elems xs <- shuffle (x `delete` elems) return (x:xs)