Skip to content

Instantly share code, notes, and snippets.

@berdario
Created February 11, 2015 09:11
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 berdario/6b89008b0b668df91270 to your computer and use it in GitHub Desktop.
Save berdario/6b89008b0b668df91270 to your computer and use it in GitHub Desktop.
A simple text index/full text search in Haskell (I wanted a simple, but non-trivial fold example)
import qualified Data.Map.Strict as M
import qualified Data.Set as S
docs = ["the quick brown fox jumps over the lazy dog",
"what does the fox say?",
"what is the meaning of life?"]
buildIndex docs = M.unionsWith S.union [foldr (flip M.insert $ S.singleton doc) M.empty $ words doc | doc <- docs]
search _ [] = S.empty
search index query = foldr1 S.intersection $ map (flip (M.findWithDefault S.empty) index) query
main = print $ search index ["fox", "what"]
where
index = buildIndex docs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment