September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
|
|
@ -1,12 +1,17 @@
|
|||
import Data.List
|
||||
import Control.Monad
|
||||
import Control.Arrow
|
||||
import Control.Monad (liftM2)
|
||||
|
||||
floydTriangle =
|
||||
liftM2
|
||||
(zipWith (liftM2 (.) enumFromTo ((pred .) . (+))))
|
||||
(scanl (+) 1)
|
||||
id
|
||||
[1 ..]
|
||||
|
||||
alignR :: Int -> Integer -> String
|
||||
alignR n = (\s -> replicate (n - length s) ' ' ++ s). show
|
||||
alignR n = (\s -> replicate (n - length s) ' ' ++ s) . show
|
||||
|
||||
floydTriangle = liftM2 (zipWith (liftM2 (.) enumFromTo ((pred.). (+)))) (scanl (+) 1) id [1..]
|
||||
|
||||
formatFT n = mapM_ (putStrLn. unwords. zipWith alignR ws) t where
|
||||
t = take n floydTriangle
|
||||
ws = map (length. show) $ last t
|
||||
formatFT :: Int -> IO ()
|
||||
formatFT n = mapM_ (putStrLn . unwords . zipWith alignR ws) t
|
||||
where
|
||||
t = take n floydTriangle
|
||||
ws = map (length . show) $ last t
|
||||
|
|
|
|||
21
Task/Floyds-triangle/Haskell/floyds-triangle-3.hs
Normal file
21
Task/Floyds-triangle/Haskell/floyds-triangle-3.hs
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
import Data.List (mapAccumL)
|
||||
|
||||
floyd :: Int -> [[Int]]
|
||||
floyd n =
|
||||
snd $
|
||||
mapAccumL
|
||||
(\start row -> (start + succ row, [start .. start + row]))
|
||||
1
|
||||
[0 .. pred n]
|
||||
|
||||
-- TEST -----------------------------------------------------------------------
|
||||
showFloyd :: [[Int]] -> String
|
||||
showFloyd xs =
|
||||
let padRight n = length >>= (<$> mappend (replicate n ' ')) . drop
|
||||
in unlines $
|
||||
(concat .
|
||||
zipWith ((. show) . padRight) ((succ . length . show) <$> last xs)) <$>
|
||||
xs
|
||||
|
||||
main :: IO ()
|
||||
main = mapM_ putStrLn $ (showFloyd . floyd) <$> [5, 14]
|
||||
Loading…
Add table
Add a link
Reference in a new issue