Rep-string: Difference between revisions

Content added Content deleted
(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.List (inits, intercalate, transpose)
<lang haskell>import Data.Bool (bool)
import Data.Bool (bool)
import Data.List (inits, intercalate, transpose)

------------------------ REP-CYCLES ----------------------


-- REP-CYCLES ---------------------------------------------
repCycles :: String -> [String]
repCycles :: String -> [String]
repCycles cs =
repCycles cs =
let n = length cs
let n = length cs
in filter
in filter ((cs ==) . take n . cycle) (tail $ inits (take (quot n 2) cs))
((cs ==) . take n . cycle)
(tail $ inits (take (quot n 2) cs))



-- TEST ---------------------------------------------------
--------------------------- 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"
"1110111011",
, "0010010010"
"0010010010",
, "1010101010"
"1010101010",
, "1111111111"
"1111111111",
, "0100101101"
"0100101101",
, "0100100"
"0100100",
, "101"
"101",
, "11"
"11",
, "00"
"00",
, "1"
"1"
]
]


-- GENERIC ------------------------------------------------
------------------------- 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 :
s : fmap (((++) . rjust w ' ' . xShow) <*> ((" -> " ++) . fxShow . f)) xs</lang>
fmap
( ((<>) . rjust w ' ' . xShow)
<*> ((" -> " <>) . fxShow . f)
)
xs</lang>
{{Out}}
{{Out}}
<pre>Longest cycles:
<pre>Longest cycles: