fork download
  1. grid :: [(Integer, Integer)]
  2. grid = f (0,0)
  3. where
  4. f (x,y) = (x,y) : f next
  5. where
  6. next | x == 0 && y == 0 = right
  7. | x > 0 && y < 0 && abs x <= abs y = left
  8. | x > 0 && y <= 0 = down
  9. | x < 0 && y < 0 && abs x >= abs y = up
  10. | x <= 0 && y < 0 = left
  11. | x < 0 && y > 0 && abs x <= abs y = right
  12. | x < 0 && y >= 0 = up
  13. | x > 0 && y > 0 && abs x > abs y = down
  14. | x >= 0 && y > 0 = right
  15. left = (x-1,y)
  16. right = (x+1,y)
  17. up = (x,y+1)
  18. down = (x,y-1)
  19.  
  20. gridInCircle :: Integer -> [(Integer, Integer)]
  21. gridInCircle r | r > 0 = [p | p@(x,y) <- takeWhile ((r,r)/=) grid, x^2+y^2<=r^2]
  22. | otherwise = []
  23.  
  24. main :: IO ()
  25. main = do
  26. print $ take 25 grid
  27. print $ gridInCircle 2
  28.  
Success #stdin #stdout 0s 6240KB
stdin
Standard input is empty
stdout
[(0,0),(1,0),(1,-1),(0,-1),(-1,-1),(-1,0),(-1,1),(0,1),(1,1),(2,1),(2,0),(2,-1),(2,-2),(1,-2),(0,-2),(-1,-2),(-2,-2),(-2,-1),(-2,0),(-2,1),(-2,2),(-1,2),(0,2),(1,2),(2,2)]
[(0,0),(1,0),(1,-1),(0,-1),(-1,-1),(-1,0),(-1,1),(0,1),(1,1),(2,0),(0,-2),(-2,0),(0,2)]