問題はこちら。 http://qiita.com/items/0ddde0164a745cd09c34
Ruby版の回答 ( http://qiita.com/items/ed5fa743164638de72b1 ) をHaskellに翻訳してみました。
左右の番兵をひとつですませる発想はなかったので、ちょっと新鮮でした。
ハッシュのかわりにパタンマッチングを使っています(なのでちょっと行数増えました)。
import Data.Char
import Test.HUnit
board = "----ABC-DEF-GHI----"
move 0 4 = 4
move 0 (-4) = -4
move 0 1 = 1
move 0 (-1) = -1
move 1 4 = 1
move 1 (-4) = -1
move 1 1 = 4
move 1 (-1) = -4
move 2 4 = -1
move 2 (-4) = 1
move 2 1 = -4
move 2 (-1) = 4
solve xs = solve' 5 4
where
solve' pos mv
| pat !! pos < 0 = []
| otherwise = let mv' = move (pat !! pos) mv
pos' = pos + mv'
in (board !! pos):(solve' pos' mv')
pat = replicate 4 (-1) ++ (pat' $ map ((subtract 48).ord) xs)
pat' [] = replicate 4 (-1)
pat' (a:b:c:t) = a:b:c:(-1):(pat' t)
main = do
runTestTT $ test $ map (\(e, a) -> e ~=? solve a) testPattern
return ()
testPattern = [ ("BEDGHIFEH", "101221102")
, ("BEH", "000000000")
, ("BCF", "111111111")
, ("BAD", "222222222")
, ("BEFIHEDGH", "000211112")
, ("BADGHIFEBCF", "221011102")
, ("BEHIFCBADEF", "201100112")
, ("BEFIH", "000111222")
, ("BC", "012012012")
, ("BEDABCFI", "201120111")
, ("BADEHGD", "220111122")
, ("BADG", "221011022")
, ("BCFIHEBA", "111000112")
, ("BEFI", "001211001")
, ("BCFEHIF", "111222012")
, ("BADEHI", "220111211")
, ("BCFEBAD", "211212212")
, ("BEFC", "002112210")
, ("BEF", "001010221")
, ("BEFIHG", "100211002")
, ("BEFCBAD", "201212121")
]