2013-12-20 55 views
0

當我鍵入此爲GHCI:臭蟲在遞歸建立的MongoDB選擇

merge [] [(pack "text") =: (pack "hey"), (pack "text") =: (pack "there")] 

我看到:

[ text: "hey", text: "there"] 

然而,當我運行此代碼:

tagsSelector :: Selector -> [String] -> Selector 
tagsSelector selector tags = case tags of 
    [] -> selector 
    t:ts -> tagsSelector (merge selector [(fieldToText Tags) =: t]) ts 

和本次測試案例:

testTagsSelector = TestCase (do 
    assertEqual "The tags selector for two tags should match..." 
     [(fieldToText Tags) =: "city", (fieldToText Tags) =: "urban"] 
      (tagsSelector [] ["city", "urban"])) 

測試失敗:

The tags selector for two tags should match... 
expected: [ tags: "city", tags: "urban"] 
but got: [ tags: "city"] 

如果我添加更多標籤tagsSelection的輸入,所產生的選擇是隻在列表中輸入的最後一個標籤的字段。遞歸有什麼問題?

回答

0
merge [pack "text" =: pack "hey"] [pack "text" =: pack "there"] 

給出

[ text: "hey"] 

所以該解決方案是:

tagsSelector :: Selector -> [String] -> Selector 
tagsSelector selector tags = case tags of 
    [] -> selector 
    t:ts -> tagsSelector (merge [] (selector ++ [(fieldToText Tags) =: t])) ts