我有一个功能:
closeTo61 :: Tactic
closeTo61 s P2 h d e b
| cs + ps < 53 = 0
| cs + ps == 61 = 100 -- Best case
| numWaysToScoreN (61 - (cs + ps)) h b == 0 = 0 -- Worst case
| numWaysToScoreN (61 - (cs + ps)) h b == 1 = 50 -- Strong case
| numWaysToScoreN (61 - (cs + ps)) h b == 2 = 70 -- Very Strong case
| numWaysToScoreN (61 - (cs + ps)) h b >= 3 = 90 -- Extremely Strong case
where
ps = scoreDom d e b
(_,cs) = s
closeTo61 s P1 h d e b
| cs + ps < 53 = 0
| cs + ps == 61 = 100
| numWaysToScoreN (61 - (cs + ps)) h b == 0 = 0
| numWaysToScoreN (61 - (cs + ps)) h b == 1 = 50
| numWaysToScoreN (61 - (cs + ps)) h b == 2 = 70
| numWaysToScoreN (61 - (cs + ps)) h b >= 3 = 90
where
ps = scoreDom d e b
(cs,_) = s
我使用两个绑定为第二个参数的每个可能输入完成此操作的唯一原因是因为在where
块中cs
模式匹配取决于此输入。
有没有办法只使用一个绑定并检查where
块内的第二个输入以使用正确的模式?
答案 0 :(得分:3)
你可以改为
closeTo61 s p h d e b
| cs + ps < 53 = 0
| ...
where
cs = chooseByP p s
chooseByP P1 = fst
chooseByP P2 = snd
虽然,因为您只使用s
和p
来确定要使用s
的哪个元素,为什么要将它们作为单独的参数?
closeTo61 cs h d e b
| ...
where
ps = scoreDom d e b
closeTo61' s p = closeTo61 (chooseByP p s)
答案 1 :(得分:2)
您可以将模式匹配移动到您定义where
的{{1}}块中:
cs
如果你这么做了(并且,既然你为closeTo61 s p h d e b
...
where cs = case p of P1 -> fst s; P2 -> snd s
和P1
定义了一个类型,你可能会),我会把这个逻辑提取到一个更有意义的辅助函数中:
P2
然后你可以做
component P1 (x, _) = x
component P2 (_, x) = x