Create a gist now

Instantly share code, notes, and snippets.

hashtags in Haskell
import Data.Char (isAlpha)
import Text.Parsec
-- | Parses exactly one hashtag (and nothing else).
hashtag = do
char '#'
many letter
-- | Parse hashtags from a string (see the previous version for
-- a list-based version).
hashtags :: String -> [String]
hashtags s = always [] parsed
where parsed = parse (sepBy hashtag noTag) "hashtags" s
-- | Parse many things that aren't '#'.
noTag = many $ noneOf "#"
-- | When the Either is a Left, always return x. Otherwise, simply
-- unwrap the Right value.
always :: c -> Either a c -> c
always x = either (const x) id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment