Created
September 17, 2011 08:59
-
-
Save missingfaktor/1223770 to your computer and use it in GitHub Desktop.
A simple problem solved in Haskell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Data.Maybe | |
| lengths :: (Int -> Bool) -> [[a]] -> [Int] | |
| lengths pred xs = mapMaybe lengthIfLong xs | |
| where | |
| lengthIfLong s = let n = length s | |
| in if pred n | |
| then Just n | |
| else Nothing | |
| main = | |
| print $ lengths (3 <) ["hello", "k", "baby"] |
Author
Nice. You rewrote my last solution using already defined functions.
Thus, justIf p = mfilter p . Just.
Author
@md2perpe: We can go one step further, and make it point-free. :-)
justIf = (. Just) . mfilterOh... :-D
A question about markup...
How do you get the code blocks in your comments to be syntax colored?
Author
@md2perpe: See "syntax highlighting" section here: http://github.github.com/github-flavored-markdown/.
Ah, I recognize that. My eyes have seen it, but my brain hasn't memorized it.
Testing:
justIf = (. Just) . mfilterUpdated my earlier posts to have syntax highlightning.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@md2perpe: +1. IMO it's common enough a case to deserve an inclusion in Prelude.
By the way, what do you think about this solution?