praxis

Various programming exercises.
Log | Files | Refs

commit 3c47ca8f5a6db7d07a8e6f5ac24a8bd8af24c582
parent 690d99a94ea8d9c3fafe97a21fbe5a822e02a26a
Author: Jared Tobin <jared@jtobin.ca>
Date:   Mon,  9 Jul 2018 07:24:37 +1200

Add Floyd's triangle.

Diffstat:
A20180522_floyd/Floyd.hs | 33+++++++++++++++++++++++++++++++++
1 file changed, 33 insertions(+), 0 deletions(-)

diff --git a/20180522_floyd/Floyd.hs b/20180522_floyd/Floyd.hs @@ -0,0 +1,33 @@ +{-# OPTIONS_GHC -Wall #-} + +import Data.List as L (unfoldr) + +floyd0 :: [[Int]] +floyd0 = loop 1 [1..] where + loop n input = + let header = take n input + footer = loop (succ n) (drop n input) + in header : footer + +floyd1 :: [[Int]] +floyd1 = L.unfoldr alg (1, 0) where + alg (m, n) = + let lbound = m + ubound = lbound + n + range = [lbound..ubound] + in Just (range, (succ ubound, succ n)) + +render :: Show a => [a] -> String +render input = case input of + [] -> "" + [h] -> show h + (h:t) -> show h ++ " " ++ render t + +main :: IO () +main = do + mapM_ (putStrLn . render) (take 5 floyd0) + + putStrLn "" + + mapM_ (putStrLn . render) (take 5 floyd1) +