Created
February 11, 2015 09:11
-
-
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)
This file contains 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 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