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

→‎{{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 Control.Arrow ((&&&))
import Data.Function (on)
import Numeric (showHex)
import Data.Char (ord)
 
 
---------------------------TEST----------------- INDICES OF DUPLICATED CHARACTERS -----------
 
duplicatedCharIndices :: String -> Maybe (Char, [Int])
Line 1,346 ⟶ 1,348:
| null duplicates = Nothing
| otherwise =
Just $
Just((,) $. ((snd . head) &&&<*> fmap fst) (head (sortOn (fst . head) duplicates))
where
duplicates =
Line 1,352 ⟶ 1,355:
groupBy (on (==) snd) $ sortOn snd $ zip [0 ..] s
 
 
---------------------------TEST----------------------------
--------------------------DISPLAY- TEST -------------------------
main :: IO ()
main =
Line 1,358 ⟶ 1,362:
fTable
"First duplicated character, if any:"
((++)fmap (<$>) show <*> ((" (" ++<>) . (++<> ")") . show . length))
(maybe
"None"
Line 1,364 ⟶ 1,368:
unwords
[ show c
, "(0x" ++<> showHex (ord c) ") at"
, 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 =
in unlines $
let rjust n c = drop . length <*> (replicate n c ++)
s : fmap (((<>) w. =rjust maximumw (length' ' . xShow) <$*> xs((" -> " <>) . fxShow . f)) xs
where
in unlines $
rjust sn :c fmap= (((++)drop . rjust w ' ' . xShow)length <*> (("replicate ->n " ++) . fxShow . f))c xs</lang>)
w = maximum (length . xShow <$> xs)
</lang>
{{Out}}
<pre>First duplicated character, if any:
9,655

edits