Search code examples
haskellchain

What do I have to change in my code to work?


I do not know what is the problem with my code. Can I get some help to understand it ?

Use longestChain to see how long the longest string in a text is made up of identical characters!

Examples:
 longestChain "2111234" == 3
 longestChain "0023212212222" == 4
 longestChain "23232323232" == 1
 longestChain "+++++!!!-------" == 7
longestChain :: String -> Int
longestChain (x:y:xs)
 | x == y = 1 + longestChain xs
 | otherwise = longestChain (y:xs)

Solution

  • More in the style of @user1984's answer. I'd still say this code is hard to read/is muddling concerns.

    longestChain :: String -> Int
    longestChain xs = go xs 0  where
      go []       curr = curr
      go [x]      curr = curr+1
      go (x:y:xs) curr | x == y    = go (y:xs) (curr+1)
                       | otherwise = max (curr+1) (go (y:xs) 0)
    

    (Here the branch go [] curr = ... is needed, because longestChain might have passed an empty string to go.)

    curr is always counting 'in arrears', so if go gets to a singleton [x] at the end of the string, that must have been the last Char of the current chain, increment the length.

    If go finds adjacent same Chars x == y, add 1 for the x, recurse on the tail.

    If go finds adjacent different chars otherwise, still add 1 for the x (it was the last Char of the current chain); start a new count for the tail. Return the max of the incremented curr cp the max from the tail.

    Tests:

    Main> longestChain ""
    0
    Main> longestChain "a"
    1
    Main> longestChain "Mississippi"
    2
    Main> longestChain "+++++!!!-------"
    7
    Main> longestChain "+++++!!!-------*"
    7