public
Last active

  • Download Gist
drunkenWalk.hs
Haskell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
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
-}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.