Last active
August 29, 2015 14:13
-
-
Save mitsuji/98f7d1c0fab08d4eaec5 to your computer and use it in GitHub Desktop.
Monad
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Prelude hiding( Left, Right ) | |
| data Heading = North | East | West | South deriving( Show, Read ) | |
| type Status = ( (Int, Int), Heading ) | |
| data Direction = Forward | Backward | Left | Right deriving( Read ) | |
| progress:: Direction -> Status -> Status | |
| progress Forward ((x, y), North) = (( x, y+1 ), North) | |
| progress Backward ((x, y), North) = (( x, y-1 ), South) | |
| progress Left ((x, y), North) = (( x-1, y ), West ) | |
| progress Right ((x, y), North) = (( x+1, y ), East ) | |
| progress Forward ((x, y), East) = (( x+1, y ), East ) | |
| progress Backward ((x, y), East) = (( x-1, y ), West ) | |
| progress Left ((x, y), East) = (( x, y+1 ), North) | |
| progress Right ((x, y), East) = (( x, y-1 ), South) | |
| progress Forward ((x, y), West) = (( x-1, y ), West ) | |
| progress Backward ((x, y), West) = (( x+1, y ), East ) | |
| progress Left ((x, y), West) = (( x, y-1 ), South) | |
| progress Right ((x, y), West) = (( x, y+1 ), North) | |
| progress Forward ((x, y), South) = (( x, y-1 ), South) | |
| progress Backward ((x, y), South) = (( x, y+1 ), North) | |
| progress Left ((x, y), South) = (( x+1, y ), East ) | |
| progress Right ((x, y), South) = (( x-1, y ), West ) | |
| pf = progress Forward | |
| pb = progress Backward | |
| pl = progress Left | |
| pr = progress Right | |
| foo = pb $ pr $ pf $ pf $ pr $ pf $ pf $ ((0,0),North) | |
| ($>) = flip ($) | |
| foof = ((0,0),North) $> pf $> pf $> pr $> pf $> pf $> pr $> pb | |
| pf':: Monad m => Status -> m Status | |
| pb':: Monad m => Status -> m Status | |
| pl':: Monad m => Status -> m Status | |
| pr':: Monad m => Status -> m Status | |
| pf' = \x -> return( pf x ) | |
| pb' = \x -> return( pb x ) | |
| pl' = \x -> return( pl x ) | |
| pr' = \x -> return( pr x ) | |
| readStatus' = \x -> return( read x ::Status ) | |
| show' = \x -> return ( show x ) | |
| fooMaybe1 = Just ((0,0),North) >>= pf' >>= pf' >>= pr' >>= pf' >>= pf' >>= pr' >>= pb' | |
| fooMaybe2 = Nothing >>= pf' >>= pf' >>= pr' >>= pf' >>= pf' >>= pr' >>= pb' | |
| pfm1 = \x -> Just Left >>= \y -> return( progress y x ) | |
| pfm2 = \x -> Nothing >>= \y -> return( progress y x ) | |
| pfl1 = \x -> [Forward,Backward,Left,Right] >>= \y -> return( progress y x ) | |
| pfl2 = \x -> [] >>= \y -> return( progress y x ) | |
| --readDirection = \x -> return( read x ::Direction) | |
| pfio = \x-> getLine >>= \y-> return( read y ::Direction) >>= \y' -> return( progress y' x ) | |
| fooList1 = [((0,0),North)] | |
| >>= pf' >>= pf' >>= pr' >>= pf' >>= pf' >>= pr' >>= pb' | |
| fooList2 = [((0,0),North),((0,0),South),((0,0),East)] | |
| -- >>= pf' >>= pf' >>= pr' >>= pf' >>= pf' >>= pr' >>= pb' | |
| >>= pf' >>= pfl1 >>= pr' >>= pf' >>= pf' >>= pr' >>= pb' | |
| fooList3 = [] | |
| >>= pf' >>= pf' >>= pr' >>= pf' >>= pf' >>= pr' >>= pb' | |
| fooIO1 = getLine >>= readStatus' | |
| >>= pf' >>= pfio >>= pr' >>= pf' >>= pf' >>= pr' >>= pb' | |
| -- >>= pf' >>= pf' >>= pr' >>= pf' >>= pf' >>= pr' >>= pb' | |
| >>= show' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment