递归建立mongodb选择的错误

时间:2013-12-20 23:51:26

标签: haskell

当我输入GHCI:

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的输入中添加更多标签,则结果选择只是在列表中输入最后一个标签的字段。递归出了什么问题?

1 个答案:

答案 0 :(得分: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