rlh*_*lhh 1 haskell functional-programming where-clause
flushPoints :: [Card] -> Integer
flushPoints cs@(c1:hd) =
if flushPointsCalc True (suitCount hd) >
flushPointsCalc False (suitCount cs)
then flushPointsCalc True (suitCount hd)
else flushPointsCalc False (suitCount cs)
Run Code Online (Sandbox Code Playgroud)
假设我有一个如上所述的函数,我该如何缩短它?
我正在考虑做一个,where hdFlush = flushPointsCalc True (suitCount hd)但我不能,因为高清被宣布在上面.
我觉得在Haskell中有一个合适的方法,考虑到它有多懒,但我不知道在哪里看.
这正是标准max函数的作用:它选择更大的值.因此,您可以将代码重写为:
flushPoints cs@(c1:hd) = max (flushPointsCalc True (suitCount hd))
(flushPointsCalc False (suitCount cs))
Run Code Online (Sandbox Code Playgroud)
如果你只是想知道如何给出一个本地名称flshPointsCalc True (suitCound hd),你确实可以使用一个where子句:
flushPoints :: [Card] -> Integer
flushPoints cs@(c1:hd) =
if hdFlush > csFlush then hdFlush else csFlush
where hdFlush = flushPointsCalc True (suitCount hd)
csFlush = flushPointsCalc False (suitCount cs)
Run Code Online (Sandbox Code Playgroud)
该cs@(c1:hd)模式位于函数where正下方的块的范围内flushPoints,因此您可以访问hd它.
| 归档时间: |
|
| 查看次数: |
81 次 |
| 最近记录: |