Skip to content

Instantly share code, notes, and snippets.

@jhickner
Last active December 13, 2015 23:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhickner/4994022 to your computer and use it in GitHub Desktop.
Save jhickner/4994022 to your computer and use it in GitHub Desktop.
import Data.List
data Dir = N | S | E | W deriving (Eq, Show)
type Coord = (Int, Int)
(|+|) (x,y) (x',y') = (x+x',y+y')
infixl 6 |+|
toDelta d = case d of
N -> (0, -1)
S -> (0, 1)
E -> (1, 0)
W -> (-1, 0)
toPath = reverse . foldl' (\p c -> head p |+| c : p) [(0,0)]
halts = isDistinct . toPath . map toDelta
where isDistinct xs = xs == nub xs
walk = [N, N, E, E, S, S, W, W]
walk2 = [N, N, N, N, N, N, N, N, N, N]
{-
> halts walk
False
> halts walk2
True
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment