Skip to content

Instantly share code, notes, and snippets.

@hoheinzollern
Created March 11, 2021 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hoheinzollern/3fb4c6c03104f35f584d47e1b78950e8 to your computer and use it in GitHub Desktop.
Save hoheinzollern/3fb4c6c03104f35f584d47e1b78950e8 to your computer and use it in GitHub Desktop.
import Data.List
data Choose a = Choose [a]
deriving (Eq, Show)
choices (Choose xs) = xs
instance Functor Choose where
fmap f (Choose xs) = Choose $ map f xs
instance Applicative Choose where
pure x = Choose [x]
(Choose []) <*> _ = Choose []
(Choose fs) <*> (Choose xs) = case ys of [] -> Choose []; _ -> Choose $ head ys
where ys = sortOn length $ filter (not.null) [[f x | f <- fs] | x <- xs] -- [[f x | x <- xs] | f <- fs]
instance Monad Choose where
return x = Choose [x]
(Choose []) >>= f = Choose []
(Choose xs) >>= f = case ys of [] -> Choose []; _ -> Choose $ head ys
where ys = sortOn length $ filter (not.null) $ map (choices.f) xs
ciao = do
x <- Choose [5,3,4]
(Choose . flip replicate 0) x
main = do
print ciao
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment