如何根据特定输入模式匹配不同的方式?

时间:2014-12-18 19:35:56

标签: haskell functional-programming

我有一个功能:

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块内的第二个输入以使用正确的模式?

2 个答案:

答案 0 :(得分:3)

你可以改为

closeTo61 s p h d e b
    | cs + ps < 53 = 0
    | ...
    where
        cs = chooseByP p s
        chooseByP P1 = fst
        chooseByP P2 = snd

虽然,因为您只使用sp来确定要使用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