模块Dominoes,其中
type Domino = [(Int, Int)]
type Hand = Domino
type Board = Hand
type End = Board
dominoes :: Domino
dominoes = [(x, y)| x <- [0..6], y <- [x..6]]
amount_to_take = 7
hand :: Domino -> Domino
hand x = take amount_to_take x我想检查Domino的任何元素是否与End的任何元素匹配。如果有则返回true,否则返回false
goesP :: Domino -> End -> Bool
goesP (h:t) (h1:t1)
| h == h1 = True
| t == t1 = True
| otherwise False发布于 2014-10-17 16:42:25
goesP :: Domino -> End -> Bool
goesP (h:t) (h1:t1)
| h == h1 = True -- Seems legit.
| t == t1 = True -- Er... this checks if the ENTIRE SUBLIST is equal.
| otherwise False -- Should be an equals sign here.另外,如果这两个列表中的任何一个为空,会发生什么?您只匹配非空的案例。
如果你想“手动”完成这项工作(即不使用现有的库函数),你可能需要这样的东西:
goesP :: Domino -> End -> Bool
goesP [] _ = False -- Ran out of Dominos to check.
goesP (d:ds) e = hunt d e || goesP ds e
where
hunt d [] = False -- Run out of Ends to check.
hunt d (e:es) = (d == e) || hunt d es如果要对库函数执行此操作,请执行以下操作:
goesP :: Domino -> End -> Bool
goesP d e = any (`elem` d) e尝试一下Hoogle,看看为什么会这样。
https://stackoverflow.com/questions/26415759
复制相似问题