praxis

Various programming exercises.
git clone git://git.jtobin.io/praxis.git
Log | Files | Refs

Floyd.hs (688B)


      1 {-# OPTIONS_GHC -Wall #-}
      2 
      3 import Data.List as L (unfoldr)
      4 
      5 floyd0 :: [[Int]]
      6 floyd0 = loop 1 [1..] where
      7   loop size input =
      8     let header = take size input
      9         footer = loop (succ size) (drop size input)
     10     in  header : footer
     11 
     12 floyd1 :: [[Int]]
     13 floyd1 = L.unfoldr alg (1, 0) where
     14   alg (lbound, size) =
     15     let ubound = lbound + size
     16         range  = [lbound..ubound]
     17     in  Just (range, (succ ubound, succ size))
     18 
     19 render :: Show a => [a] -> String
     20 render input = case input of
     21   []    -> ""
     22   [h]   -> show h
     23   (h:t) -> show h ++ " " ++ render t
     24 
     25 main :: IO ()
     26 main = do
     27   mapM_ (putStrLn . render) (take 5 floyd0)
     28 
     29   putStrLn ""
     30 
     31   mapM_ (putStrLn . render) (take 5 floyd1)
     32