2015年1月2日金曜日

150102(5)

Haskell


すごいH本

第13章13.6

type KnightPos = (Int, Int)

moveKnight :: KnightPos -> [KnightPos]
moveKnight (x, y) = filter onBoard
    [(x + 1, y + 2),(x + 1, y - 2),(x + 2, y + 1),(x + 2, y - 1),(x - 1, y + 2),(x - 1, y - 2),(x - 2, y + 1),(x - 2, y - 1)]
    where onBoard (x', y') = x' `elem` [1..8] && y' `elem` [1..8]

in3 :: KnightPos -> [KnightPos]
in3 start = do
    first <- moveKnight start
    second <- moveKnight first
    moveKnight second
 
canReachIn3 :: KnightPos -> KnightPos -> Bool
canReachIn3 start end = end `elem` in3 start

Prelude> :l 13.6
[1 of 1] Compiling Main             ( 13.6.hs, interpreted )
Ok, modules loaded: Main.
*Main> canReachIn3 (6, 2) (6, 1)
True
*Main> canReachIn3 (6, 2) (7, 3)
False

0 件のコメント:

コメントを投稿

注: コメントを投稿できるのは、このブログのメンバーだけです。