Determine if a string has all unique characters: Difference between revisions
Determine if a string has all unique characters (view source)
Revision as of 00:58, 20 December 2020
, 3 years ago→{{header|Haskell}}: Minor tidying.
m (→{{header|Haskell}}: Minor tidying.) |
|||
Line 1,444:
import Numeric (showHex)
import Data.Char (ord)
----------- INDICES OF ANY DUPLICATED CHARACTERS ---------
duplicatedCharIndices :: String -> Maybe (Char, [Int])
Line 1,453 ⟶ 1,456:
((1 <) . length)
(foldl' --'
(\a (i, c) -> M.insert c (maybe [i] (
M.empty
(zip [0 ..] xs))))
Line 1,464 ⟶ 1,467:
Nothing
(foldl' --'
(\a (i, c) -> M.insert c (maybe [i] (
M.empty
(zip [0 ..] xs))
Line 1,474 ⟶ 1,477:
| otherwise = Just (c, ys)
--------------------------- TEST
main :: IO ()
main =
Line 1,480 ⟶ 1,483:
fTable
"First duplicated character, if any:"
((
(maybe
"None"
Line 1,486 ⟶ 1,489:
unwords
[ show c
, "(0x"
, intercalate ", " (show <$> ixs)
]))
Line 1,492 ⟶ 1,495:
["", ".", "abcABC", "XYZ ZYX", "1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ"]
-------------------------
fTable :: String -> (a -> String) -> (b -> String) -> (a -> b) -> [a] -> String
fTable s xShow fxShow f xs =
unlines $
s : fmap (((
where
rjust n c = drop . length <*> (replicate n c
w = maximum (length . xShow <$> xs)</lang>
{{Out}}
|