Skip to content

Instantly share code, notes, and snippets.

@MiyamonY
Created March 5, 2015 02:17
Show Gist options
  • Save MiyamonY/5b682bf2bac12c0f2058 to your computer and use it in GitHub Desktop.
Save MiyamonY/5b682bf2bac12c0f2058 to your computer and use it in GitHub Desktop.
do
import Monad
import Control.Monad
foo :: Maybe String
foo = Just 3 >>= (\ x -> Just "!" >>= \ y -> Just $ show x ++ y)
bar :: Maybe String
bar = do
x <- Just 3
y <- Just "!"
return $ show x ++ y
marySue :: Maybe Bool
marySue = do
x <- Just 9
return $ x > 8
routine :: Maybe Pole
routine = do
start <- return (0, 0)
first <- landLeft 2 start
second <- landRight 2 first
landLeft 1 second
justH :: Maybe Char
justH = do
(x : xs) <- Just "hello"
return x
wopwop :: Maybe Char
wopwop = do
(x : xs) <- Just ""
return x
listOfTuples :: [(Int, Char)]
listOfTuples = do
n <- [1,2]
ch <- ['a', 'b']
return (n, ch)
sevensOnly :: [Int]
sevensOnly = do
x <- [1..50]
guard ('7' `elem` show x)
return x
type KnightPos = (Int, Int)
moveKnight :: KnightPos -> [KnightPos]
moveKnight (c, r) = do
(c', r') <- [(c + i, r + j) | i <- [-2, 2], j <- [-1, 1]]
++ [(c + i, r + j) | i <- [-1, 1], j <- [-2, 2]]
guard (c' `elem` [1..8] && r' `elem` [1..8])
return (c', r')
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment