[Haskell-cafe] Translating an imperative algorithm - negascout

wren ng thornton wren at freegeek.org
Fri Feb 27 21:12:16 EST 2009


Holger Siegel wrote:
>>    loop alpha b (c:cs) 
>>        = let alpha' = max(alpha, - negascout c d (-b) (-alpha))
>>          in if alpha' >= beta
>>             then alpha'
>>             else if alpha' >= b
>>                  then let alpha'' = - negascout c d (-beta) (-alpha')
>>                       in if alpha'' >= beta
>>                          then alpha''
>>                          else loop alpha'' (alpha'' + 1) cs 
>>                  else loop alpha' (alpha' + 1) cs
>>    loop alpha _ [] = alpha


Guard-izing and where-izing that for more clarity:

     > loop alpha _ []     = alpha
     > loop alpha b (c:cs) = result
     >    where
     >    alpha' = max(alpha, - negascout c d (-b) (-alpha))
     >    result
     >        | alpha' >= beta  = alpha'
     >        | alpha' >= b     = result'
     >        | otherwise       = loop alpha' (alpha' + 1) cs
     >        where
     >        result'
     >            | alpha'' >= beta = alpha''
     >            | otherwise       = loop alpha'' (alpha'' + 1) cs
     >            where
     >            alpha'' = - negascout c d (-beta) (-alpha')

Which makes it obvious that the "result" function is something inlined 
into itself. Uninlining may make the code clearer still, or maybe not.

-- 
Live well,
~wren


More information about the Haskell-Cafe mailing list