【发布时间】:2019-10-12 23:26:49
【问题描述】:
我在这里第 2 行的代码中得到了一个非详尽的模式,我认为它具有侵入性,因为我需要它让我的递归在某个时候停止。 它说模式不匹配:[] _
allergyFree :: [Ingredient] -> [Cupcake] -> [Cupcake]
allergyFree (y:ys) [] = []
allergyFree (y:ys) (c:cs) = if(not(igPresent (y:ys) recipeN)) then ([c] ++ (allergyFree (y:ys) cs))
else (allergyFree (y:ys) cs)
where
CC (P priceN) recipeN = c
igPresent :: [Ingredient] -> [Ingredient] -> Bool
igPresent [] recipeN = False
igPresent (y:ys) recipeN = if (y `elem` recipeN) then True
else igPresent ys recipeN
任何帮助将不胜感激!
附:我对haskell很陌生
【问题讨论】:
-
问题:当您拨打
allergyFree [] _somethingelse时会发生什么?这是您的非详尽模式。 -
是的,我会出错,我意识到出了什么问题,并明确定义了所有列表为空的情况,谢谢!
-
我希望这可以写得更简洁;例如类似
allergyFree allergies = filter (\(CC _ recipe) -> all (`notElem` recipe) allergies)。这种方式对我来说似乎也很可读。 -
使用
-Wall启用警告。如果您这样做了,GHC 会就您未能处理的案例向您发出警告。
标签: haskell