Haskell function :: [Name] -> [[(Name, Bool)]]

后端 未结 1 654
花落未央
花落未央 2021-01-23 18:53

Given the following:

type Name = String
envs :: [Name] -> [[(Name , Bool)]]

I have to implement \'envs\' so that given a list of Names, it r

相关标签:
1条回答
  • 2021-01-23 19:34

    What you want to do is get all combinations of True and False for the number of names, which can be done easily with replicateM specialised to lists:

    replicateM (length names) [False, True]
    

    E.g. if length names == 2, then this produces:

    [ [False, False]
    , [False, True]
    , [True, False]
    , [True, True]
    ]
    

    What remains is just to zip each of these with the names themselves:

    envs names =
      [ zip names values
      | let n = length names
      , values <- replicateM n [False, True]
      ]
    

    For envs ["P", "Q"] this produces:

    [ [("P", False), ("Q", False)]
    , [("P", False), ("Q", True)]
    , [("P", True), ("Q", False)]
    , [("P", True), ("Q", True)]
    ]
    

    And it works for any number of inputs, even 0, for which your original implementation would fail since you don’t match []. It always returns 2n assignments:

    -- 2^0 == 1
    envs [] == [[]]
    
    -- 2^3 == 8
    envs ["P", "Q", "R"] ==
      [ [("P", False), ("Q", False), ("R", False)]
      , [("P", False), ("Q", False), ("R", True)]
      , [("P", False), ("Q", True), ("R", False)]
      , [("P", False), ("Q", True), ("R", True)]
      , [("P", True), ("Q", False), ("R", False)]
      , [("P", True), ("Q", False), ("R", True)]
      , [("P", True), ("Q", True), ("R", False)]
      , [("P", True), ("Q", True), ("R", True)]
      ]
    
    0 讨论(0)
提交回复
热议问题