diff options
author | Joe Zhao <ztuowen@gmail.com> | 2014-08-09 10:58:03 +0800 |
---|---|---|
committer | Joe Zhao <ztuowen@gmail.com> | 2014-08-09 10:58:03 +0800 |
commit | 5bdaa1e4ffe40add10f000ee993e6b500c419a37 (patch) | |
tree | 46a624488ece8d37fa3580422234ed2b4acc6728 /sandbox.hs | |
parent | 01aa73b269f7ad780233be338affdf3c9288b1ed (diff) | |
download | haskbox-old-5bdaa1e4ffe40add10f000ee993e6b500c419a37.tar.gz haskbox-old-5bdaa1e4ffe40add10f000ee993e6b500c419a37.tar.bz2 haskbox-old-5bdaa1e4ffe40add10f000ee993e6b500c419a37.zip |
add files from home for previous chapters and sandboxes
Diffstat (limited to 'sandbox.hs')
-rw-r--r-- | sandbox.hs | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/sandbox.hs b/sandbox.hs new file mode 100644 index 0000000..f1a3213 --- /dev/null +++ b/sandbox.hs @@ -0,0 +1,28 @@ +import Control.Monad +import Data.Monoid + +dotest x = do + if x then return "Test" + else return "OK" + +type KnightPos = (Int, Int) + +moveKnight :: KnightPos -> [KnightPos] +moveKnight (c, r) = do + (c', r') <- [(c+2,r-1),(c+2,r+1),(c-2,r-1),(c-2,r+1) + ,(c+1,r-2),(c+1,r+2),(c-1,r-2),(c-1,r+2)] + guard (c' `elem` [1..8] && r' `elem` [1..8]) + return (c', r') + +filterDup :: [KnightPos] -> [KnightPos] +filterDup [] = [] +filterDup (x:xr) = x:(filterDup $ filter (x/=) xr) + +singleStep x = filterDup $ x >>= moveKnight + +multiStep :: Int -> [KnightPos] -> [KnightPos] +multiStep 0 x = x +multiStep n x = multiStep (n-1) (singleStep x) + +applyLog :: (Monoid m) => (a, m) -> (a -> (b, m)) -> (b, m) +applyLog (x, log) f = let (y, newLog) = f x in (y, log `mappend` newLog) |