Determine if a string has all unique characters: Difference between revisions
Determine if a string has all unique characters (view source)
Revision as of 18:39, 12 November 2020
, 3 years ago→{{header|Haskell}}: Tidied, replaced an Arrow with an Applicative
(Determine if a string has all unique characters en FreeBASIC) |
(→{{header|Haskell}}: Tidied, replaced an Arrow with an Applicative) |
||
Line 1,337:
<lang haskell>import Data.List (groupBy, intercalate, sortOn)
import Data.Function (on)
import Numeric (showHex)
import Data.Char (ord)
duplicatedCharIndices :: String -> Maybe (Char, [Int])
Line 1,346 ⟶ 1,348:
| null duplicates = Nothing
| otherwise =
Just $
where
duplicates =
Line 1,352 ⟶ 1,355:
groupBy (on (==) snd) $ sortOn snd $ zip [0 ..] s
▲---------------------------TEST----------------------------
main :: IO ()
main =
Line 1,358 ⟶ 1,362:
fTable
"First duplicated character, if any:"
(
(maybe
"None"
Line 1,364 ⟶ 1,368:
unwords
[ show c
, "(0x"
, intercalate ", " (show <$> ixs)
]))
Line 1,370 ⟶ 1,374:
["", ".", "abcABC", "XYZ ZYX", "1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ"]
▲--------------------------DISPLAY--------------------------
------------------------- DISPLAY ------------------------
fTable :: String -> (a -> String) -> (b -> String) -> (a -> b) -> [a] -> String
fTable s xShow fxShow f xs =
s : fmap (((<>)
where
▲ in unlines $
rjust
w = maximum (length . xShow <$> xs)
</lang>
{{Out}}
<pre>First duplicated character, if any:
|