Happened across it while looking for Haskell-related blogs to link to: from Derek Wyatt's blog, a post titled "Haskell sequence over functions -- explained" gives this example:
sequence [(> 4), (< 10), odd] 7
gives the result we would get from our wonkyMap with the same arguments:
[True, True, True]
What is this sequence?
sequence :: Monad m => [m a] -> m [a]
If m is a monad, then sequence will map a [m a] to a m [a].
So, our functions must have the type m a for some m and a. Let's inquire with ghci once again.
:t sequence [(> 4), (< 10), odd]
sequence [(> 4), (< 10), odd] :: Integral a => a -> [Bool]
Aha. That reminds me of the exercise in Typeclassopedia: show that (->) r is a functor. (WTF? That's the type of functions that take an argument of type r.) Turns out that not only is it a functor, it's a monad; Typeclassopedia refers to it as the "reader monad".
Unfortunately, we have dueling definitions here. Mr. Wyatt's blog post uses the Learn You a Haskell for Great Good! definition, as opposed to what we see from ghci, though clearly the end result is the same. Let's carry on with the version that ghci uses, and you neither want nor need to see my long, rambling, not-yet-successful attempt to convince myself that (->) r really is a monad. We'll just suppose it is for now, and see what the consequences are.
Haskell's type inference takes the most general possible type. The functions we have here are all Integral a => a -> Bool. In fact, I suspect Integral b => b is r for this example, using b because the sequence declaration has already used a, and I don't need help getting more confused. For us, the a mentioned in sequence's declaration is Bool, and in our case, we have
sequence :: Integral b => [b -> Bool] -> b -> [Bool]
which is exactly what we wanted for our solution to the problem in that online Haskell course.
So, bye-bye wonkyMap; we can rewrite strong as
strong password = all $ sequence constraints password
where constraints = [(>= 15) . length,
...and I definitely need to get my head around monads, if for no other reason than to come up with alternative Hoogle search strings. :)
You've probably heard about how the notion of sum types (e.g. Algol 68 union s, Rust enum s, Haskell type s) and product types (e.g. tup...
As I've mentioned recently, I'm now in a Meetup for people in the Des Moines, IA area where we're seriously doing the problems a...
If I don't already have the Haskell subreddit link over on the right, I'll add it ASAP. This evening a Haskell beginner posted a...
While going through old papers, I found something I wrote as a follow-up to an article by Aaron Banerjee in the February 1999 issue of t he...