Rep-string: Difference between revisions
Content added Content deleted
MaiconSoft (talk | contribs) (Added Delphi example) |
m (→{{header|Haskell}}: Tidied alternative version. Applied Ormolu.) |
||
Line 1,390: | Line 1,390: | ||
Or, alternatively: |
Or, alternatively: |
||
<lang haskell>import Data. |
<lang haskell>import Data.Bool (bool) |
||
import Data. |
import Data.List (inits, intercalate, transpose) |
||
⚫ | |||
⚫ | |||
repCycles :: String -> [String] |
repCycles :: String -> [String] |
||
repCycles cs = |
repCycles cs = |
||
let n = length cs |
let n = length cs |
||
in filter |
|||
⚫ | |||
((cs ==) . take n . cycle) |
|||
⚫ | |||
- |
--------------------------- TEST ------------------------- |
||
main :: IO () |
main :: IO () |
||
main = |
main = |
||
putStrLn $ |
putStrLn $ |
||
fTable |
fTable |
||
"Longest cycles:\n" |
"Longest cycles:\n" |
||
id |
id |
||
((flip bool "n/a" . last) <*> null) |
((flip bool "n/a" . last) <*> null) |
||
repCycles |
repCycles |
||
[ "1001110011" |
[ "1001110011", |
||
"1110111011", |
|||
"0010010010", |
|||
"1010101010", |
|||
"1111111111", |
|||
"0100101101", |
|||
"0100100", |
|||
"101", |
|||
"11", |
|||
"00", |
|||
"1" |
|||
] |
] |
||
- |
------------------------- GENERIC ------------------------ |
||
fTable :: |
|||
fTable :: String -> (a -> String) -> (b -> String) -> (a -> b) -> [a] -> String |
|||
String -> |
|||
(a -> String) -> |
|||
(b -> String) -> |
|||
(a -> b) -> |
|||
[a] -> |
|||
String |
|||
fTable s xShow fxShow f xs = |
fTable s xShow fxShow f xs = |
||
let rjust n c = drop . length <*> (replicate n c |
let rjust n c = drop . length <*> (replicate n c <>) |
||
w = maximum (length . xShow <$> xs) |
w = maximum (length . xShow <$> xs) |
||
in unlines $ |
in unlines $ |
||
s : |
|||
⚫ | |||
fmap |
|||
( ((<>) . rjust w ' ' . xShow) |
|||
⚫ | |||
) |
|||
xs</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre>Longest cycles: |
<pre>Longest cycles: |