Skip to content

Instantly share code, notes, and snippets.

@jaspervdj
Created September 30, 2020 11:18
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 jaspervdj/1cd04abe2f61b60e42309ec6351f756f to your computer and use it in GitHub Desktop.
Save jaspervdj/1cd04abe2f61b60e42309ec6351f756f to your computer and use it in GitHub Desktop.
It's traverse
-- Imagine you're trying to guess someone's PIN code, and you have limited
-- info on each digit, e.g. if the digit is odd or even.
type Predicate a = a -> Bool
-- This is the info we have about the PIN code.
info :: [Predicate Int]
info = [odd, even, even, odd]
-- A helper to produce all digits satisfying a predicate.
digits :: Predicate Int -> [Int]
digits f = filter f [0 .. 9]
-- It's traverse!
guesses :: [[Int]]
guesses = traverse digits info
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment