Padovan n-step number sequences: Difference between revisions
m
→{{header|Haskell}}: Tidied and updated output.
m (→{{header|Haskell}}: Tidied and updated output.) |
|||
Line 625:
=={{header|Haskell}}==
<lang haskell>import Data.Bifunctor (second)
import Data.List (transpose, uncons, unfoldr)
------------------ PADOVAN N-STEP SERIES -----------------
Line 632:
padovans n
| 0 > n = []
| otherwise = unfoldr
where
f =▼
( fmap▼
. second▼
. flip (<>)▼
. pure▼
. sum▼
. take n▼
)▼
<*> uncons▼
xs
| 3 > n = repeat 1
| otherwise = padovans $ pred n
recurrence :: Int -> [Int] -> Maybe (Int, [Int])
recurrence n =
( fmap
)
--------------------------- TEST -------------------------
Line 651 ⟶ 653:
main =
putStrLn $
"Padovan N-step series:\n\n"
[
<> fmap show (take 15 $ padovans n)
▲ )
------------------------ FORMATTING ----------------------
(Int -> Char -> String -> String) -> [[String]] -> String
spacedTable aligned rows =
let columnWidths =
(maximum . fmap length)
(transpose rows)
in unlines $▼
▲ unlines $
(unwords . zipWith (`aligned` ' ') columnWidths)
▲ <*> ((" ->" <>) . fxShow . f)
▲ )
justifyRight :: Int -> a -> [a] -> [a]
Line 682:
{{Out}}
<pre>Padovan N-step series:
8 -> 1 1 1 2 3 5 8 13 21 34 54 87 140 225 362</pre>
=={{header|JavaScript}}==
|