File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import Control.Monad (guard )
2+ import Data.List (last )
3+
4+ type KnightPos = (Int , Int )
5+
6+ moveKnight :: KnightPos -> [KnightPos ]
7+ moveKnight (c, r) = do
8+ (c', r') <-
9+ [ (c + 2 , r - 1 )
10+ , (c + 2 , r + 1 )
11+ , (c - 2 , r - 1 )
12+ , (c - 2 , r + 1 )
13+ , (c + 1 , r - 2 )
14+ , (c + 1 , r + 2 )
15+ , (c - 1 , r - 2 )
16+ , (c - 1 , r + 2 )
17+ ]
18+ guard (c' `elem` [1 .. 8 ] && r' `elem` [1 .. 8 ])
19+ return (c', r')
20+
21+ paths :: KnightPos -> [[KnightPos ]]
22+ paths start =
23+ let addMoves :: [KnightPos ] -> [[KnightPos ]]
24+ addMoves ls@ (x: _) = (: ls) <$> moveKnight x
25+ pathsFrom :: [[KnightPos ]] -> [[KnightPos ]]
26+ pathsFrom paths = paths ++ pathsFrom (paths >>= addMoves)
27+ in map reverse $ pathsFrom (addMoves [start])
28+
29+ pathsIn :: KnightPos -> Int -> [[KnightPos ]]
30+ pathsIn start n =
31+ let paths' = paths start
32+ bounded = takeWhile ((<= n + 1 ) . length )
33+ in bounded paths'
34+
35+ pathsTo :: KnightPos -> KnightPos -> [[KnightPos ]]
36+ pathsTo start end =
37+ let ended = filter ((== end) . last )
38+ in ended $ paths start
39+
40+ minMovesTo :: KnightPos -> KnightPos -> Int
41+ minMovesTo start end = subtract 1 . length . head $ pathsTo start end
Original file line number Diff line number Diff line change 1+ import Control.Monad
2+
3+ -- Monad laws
4+ -- 1. left identity: return x >>= f = f x
5+ -- 2. right identity: m >>= return = m
6+ -- 3. associativity: (m >>= f) >>= g = m >>= (\x -> f x >>= g)
7+ h =
8+ let f x = [x, - x]
9+ g x = [x * 3 , x * 2 ]
10+ in f <=< g
11+
12+ type Birds = Int
13+
14+ type Pole = (Birds , Birds )
15+
16+ landLeft :: Birds -> Pole -> Maybe Pole
17+ landLeft n (left, right)
18+ | abs ((left + n) - right) < 4 = Just (left + n, right)
19+ | otherwise = Nothing
20+
21+ landRight :: Birds -> Pole -> Maybe Pole
22+ landRight n (left, right)
23+ | abs (left - (right + n)) < 4 = Just (left, right + n)
24+ | otherwise = Nothing
25+
26+ -- try:
27+ -- return (0, 0) >>= landLeft 1 >>= landRight 4 >>= landRight (-2)
28+ -- return (0, 0) >>= landLeft 1 >>= landRight 4 >>= landLeft (-1) >>= landRight (-2)
29+ banana :: Pole -> Maybe Pole
30+ banana _ = Nothing
31+
32+ routine :: Maybe Pole
33+ routine = do
34+ let start = (0 , 0 )
35+ first <- landLeft 2 start
36+ second <- landRight 2 first
37+ landLeft 1 second
38+
39+ -- or:
40+ -- return (0, 0) >>= landLeft 2 >>= landRight 2 >>= landLeft 1
41+ routine' :: Maybe Pole
42+ routine' = do
43+ let start = (0 , 0 )
44+ first <- landLeft 2 start
45+ Nothing
46+ second <- landRight 2 first
47+ landLeft 1 second
48+ -- or:
49+ -- return (0, 0) >>= landLeft 2 >>= landRight 2 >> Nothing >>= landLeft 1
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ maintainer: example@example.com
88copyright : 2019 Author name here
99license : BSD3
1010license-file : LICENSE
11+ cabal-version : >= 1.2
1112build-type : Simple
1213extra-source-files :
1314 README.md
You can’t perform that action at this time.
0 commit comments