Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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