Rep-string: Difference between revisions
m
→{{header|Haskell}}: Tidied alternative version. Applied Ormolu.
MaiconSoft (talk | contribs) (Added Delphi example) |
m (→{{header|Haskell}}: Tidied alternative version. Applied Ormolu.) |
||
Line 1,390:
Or, alternatively:
<lang haskell>import Data.
import Data.
▲-- 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)
-
main :: IO ()
main =
putStrLn $
fTable
"Longest cycles:\n"
id
((flip bool "n/a" . last) <*> null)
repCycles
[ "1001110011",
]
-
fTable ::
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)
)
xs</lang>
{{Out}}
<pre>Longest cycles:
|