Jump to content

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

m
→‎{{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] (++<> [i]) (M.lookup c a)) a)
M.empty
(zip [0 ..] xs))))
Line 1,464 ⟶ 1,467:
Nothing
(foldl' --'
(\a (i, c) -> M.insert c (maybe [i] (++<> [i]) (M.lookup c a)) a)
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:"
((++<>) <$> show <*> ((" (" ++<>) . (++<> ")") . show . length))
(maybe
"None"
Line 1,486 ⟶ 1,489:
unwords
[ show c
, "(0x" ++<> showHex (ord c) ") at"
, intercalate ", " (show <$> ixs)
]))
Line 1,492 ⟶ 1,495:
["", ".", "abcABC", "XYZ ZYX", "1234567890ABCDEFGHIJKLMN0PQRSTUVWXYZ"]
 
-------------------------- DISPLAY-- ------------------------
fTable :: String -> (a -> String) -> (b -> String) -> (a -> b) -> [a] -> String
fTable s xShow fxShow f xs =
unlines $
s : fmap (((++<>) . rjust w ' ' . xShow) <*> ((" -> " ++<>) . fxShow . f)) xs
where
rjust n c = drop . length <*> (replicate n c ++<>)
w = maximum (length . xShow <$> xs)</lang>
{{Out}}
9,659

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.