Skip to content

Instantly share code, notes, and snippets.

@jmg-duarte
Created February 6, 2018 21:05
Show Gist options
  • Save jmg-duarte/e9bb4ae34104273eca4336e20657a7af to your computer and use it in GitHub Desktop.
Save jmg-duarte/e9bb4ae34104273eca4336e20657a7af to your computer and use it in GitHub Desktop.
--https://mightybyte.github.io/monad-challenges/pages/ex1-4.html
type Gen a = Seed -> (a, Seed)
randPair :: Gen (Char, Integer)
randPair seed =
let (c, s0) = randLetter seed in
let (i, s1) = rand s0 in
((c, i), s1)
generalPair :: Gen a -> Gen b -> Gen (a,b)
generalPair f g = pairValues . applyOnSeed g . f
generalPair' :: Gen a -> Gen b -> Gen (a, b)
generalPair' f g = generalB f g pairValues
generalA :: (a -> b) -> Gen a -> Gen b
generalA f g = applyOnVal f . g
generalB :: Gen a -> Gen b -> ((a, (b, Seed)) -> (c, Seed)) -> Gen c
generalB f g h = h . applyOnSeed g . f
applyOnSeed :: Gen b -> (a, Seed) -> (a, (b, Seed))
applyOnSeed f (x, y) = (x, f y)
applyOnVal :: (a -> b) -> (a, Seed) -> (b, Seed)
applyOnVal f (x, y) = (f x, y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment