Skip to content

Instantly share code, notes, and snippets.

@heyLu
Created November 21, 2011 19:37
Show Gist options
  • Save heyLu/1383667 to your computer and use it in GitHub Desktop.
Save heyLu/1383667 to your computer and use it in GitHub Desktop.
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