Determine if a string has all unique characters: Difference between revisions

Content added Content deleted
m (→‎{{header|Haskell}}: Minor tidying.)
Line 1,444: Line 1,444:
import Numeric (showHex)
import Numeric (showHex)
import Data.Char (ord)
import Data.Char (ord)


----------- INDICES OF ANY DUPLICATED CHARACTERS ---------


duplicatedCharIndices :: String -> Maybe (Char, [Int])
duplicatedCharIndices :: String -> Maybe (Char, [Int])
Line 1,453: Line 1,456:
((1 <) . length)
((1 <) . length)
(foldl' --'
(foldl' --'
(\a (i, c) -> M.insert c (maybe [i] (++ [i]) (M.lookup c a)) a)
(\a (i, c) -> M.insert c (maybe [i] (<> [i]) (M.lookup c a)) a)
M.empty
M.empty
(zip [0 ..] xs))))
(zip [0 ..] xs))))
Line 1,464: Line 1,467:
Nothing
Nothing
(foldl' --'
(foldl' --'
(\a (i, c) -> M.insert c (maybe [i] (++ [i]) (M.lookup c a)) a)
(\a (i, c) -> M.insert c (maybe [i] (<> [i]) (M.lookup c a)) a)
M.empty
M.empty
(zip [0 ..] xs))
(zip [0 ..] xs))
Line 1,474: Line 1,477:
| otherwise = Just (c, ys)
| otherwise = Just (c, ys)


---------------------------TEST----------------------------
--------------------------- TEST -------------------------
main :: IO ()
main :: IO ()
main =
main =
Line 1,480: Line 1,483:
fTable
fTable
"First duplicated character, if any:"
"First duplicated character, if any:"
((++) <$> show <*> ((" (" ++) . (++ ")") . show . length))
((<>) <$> show <*> ((" (" <>) . (<> ")") . show . length))
(maybe
(maybe
"None"
"None"
Line 1,486: Line 1,489:
unwords
unwords
[ show c
[ show c
, "(0x" ++ showHex (ord c) ") at"
, "(0x" <> showHex (ord c) ") at"
, intercalate ", " (show <$> ixs)
, intercalate ", " (show <$> ixs)
]))
]))
Line 1,492: Line 1,495:
["", ".", "abcABC", "XYZ ZYX", "1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ"]
["", ".", "abcABC", "XYZ ZYX", "1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ"]


--------------------------DISPLAY--------------------------
------------------------- DISPLAY ------------------------
fTable :: String -> (a -> String) -> (b -> String) -> (a -> b) -> [a] -> String
fTable :: String -> (a -> String) -> (b -> String) -> (a -> b) -> [a] -> String
fTable s xShow fxShow f xs =
fTable s xShow fxShow f xs =
unlines $
unlines $
s : fmap (((++) . rjust w ' ' . xShow) <*> ((" -> " ++) . fxShow . f)) xs
s : fmap (((<>) . rjust w ' ' . xShow) <*> ((" -> " <>) . fxShow . f)) xs
where
where
rjust n c = drop . length <*> (replicate n c ++)
rjust n c = drop . length <*> (replicate n c <>)
w = maximum (length . xShow <$> xs)</lang>
w = maximum (length . xShow <$> xs)</lang>
{{Out}}
{{Out}}