Rep-string: Difference between revisions

m
→‎{{header|Haskell}}: Tidied alternative version. Applied Ormolu.
(Added Delphi example)
m (→‎{{header|Haskell}}: Tidied alternative version. Applied Ormolu.)
Line 1,390:
 
Or, alternatively:
<lang haskell>import Data.ListBool (inits, intercalate, transposebool)
import Data.BoolList (boolinits, intercalate, transpose)
 
-- REP-CYCLES ---------------------- REP-CYCLES ----------------------
 
-- REP-CYCLES ---------------------------------------------
repCycles :: String -> [String]
repCycles cs =
let n = length cs
in filter
in filter ((cs ==) . take n . cycle) (tail $ inits (take (quot n 2) cs))
((cs ==) . take n . cycle)
in filter ((cs ==) . take n . cycle) (tail $ inits (take (quot n 2) cs))
 
 
-- TEST -------------------------- TEST -------------------------
main :: IO ()
main =
putStrLn $
fTable
"Longest cycles:\n"
id
((flip bool "n/a" . last) <*> null)
repCycles
[ "1001110011",
, "1110111011",
, "0010010010",
, "1010101010",
, "1111111111",
, "0100101101",
, "0100100",
, "101",
, "11",
, "00",
, "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 =
let rjust n c = drop . length <*> (replicate n c ++<>)
w = maximum (length . xShow <$> xs)
in unlines $
s :
s : fmap (((++) . rjust w ' ' . xShow) <*> ((" -> " ++) . fxShow . f)) xs</lang>
fmap
( ((<>) . rjust w ' ' . xShow)
s : fmap (((++) . rjust w ' ' . xShow) <*> ((" -> " ++<>) . fxShow . f)) xs</lang>
)
xs</lang>
{{Out}}
<pre>Longest cycles:
9,659

edits