Skip to content

Instantly share code, notes, and snippets.

@OJ
Created October 26, 2011 01:38
Show Gist options
  • Save OJ/1315146 to your computer and use it in GitHub Desktop.
Save OJ/1315146 to your computer and use it in GitHub Desktop.
Solution to the Ruler Problem in Haskll
ruler :: Int -> Int -> String
ruler h s = ruler' (2 ^ h * s + 1) h 1 [] []
where
outChar c r = if (c `mod` (2 ^ r)) == 1 then '|' else ' '
ruler' _ 0 _ _ a = unlines $ reverse a
ruler' w r c l a = let nl = (outChar c r) : l
in if c == w then ruler' w (r - 1) 1 [] (nl : a)
else ruler' w r (c + 1) nl a
-- to diplay results:
-- putStrLn $ ruler 6 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment