Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6905679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:12:49+00:00 2026-05-27T08:12:49+00:00

I’ve got a problem with this code, it should count the longest substring of

  • 0

I’ve got a problem with this code, it should count the longest substring of the same letter in a string, but there is an error:

*** Exception: test.hs:(15,0)-(21,17): 
Non-exhaustive patterns in function countLongest'

I know that is the wrong types problem, but i dont know where is the error, or how to find or debug it

countLongest :: (Eq a) => [a] -> Int
countLongest' :: (Eq a) => Int -> Int -> [a] -> Int

countLongest a = countLongest' 0 0 a
countLongest' n max (y:x:ys)
        | y == x = countLongest' (n+1) max (x:ys)
        | n > max = countLongest' 0 (n) (x:ys)
        | otherwise = countLongest' 0 (max) (x:ys)
countLongest' n max []
        | n > max = n
        | otherwise = max
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T08:12:49+00:00Added an answer on May 27, 2026 at 8:12 am

    It looks like you’re missing the case where there’s a one element list:

    countLongest' n max (y:ys)
        | ... etc. ...
        | otherwise = ....
    

    Here’s a contrived example similar to yours:

    f [] = 3         -- matches an empty list
    f (a:b:bs) = 4   -- matches a list with at least two elements
    

    Examples:

    Prelude> :load myfile.hs 
    [1 of 1] Compiling Main             ( myfile.hs, interpreted )
    Ok, modules loaded: Main.
    *Main> f [3]
    *** Exception: myfile.hs:(3,0)-(4,13): Non-exhaustive patterns in function f
    
    *Main> f []
    3
    *Main> f [1,2,3,4,5]
    4
    *Main> 
    

    So it succeeds with 0 and 2 elements in the list, but fails when there’s exactly one element.


    Note that this behavior is not unique to lists. Here’s an example using Maybe:

    g :: Maybe x -> x
    g (Just x) = x
    

    Examples:

    *Main> g (Just 4)
    4
    *Main> g Nothing 
    *** Exception: myfile.hs:6:0-13: Non-exhaustive patterns in function g
    

    This happened because there’s two constructors for Maybe, Just <something> and Nothing. We didn’t provide a case for Nothing, so when we passed that into g, it didn’t work!


    Check out this question and its answers for information on getting a little help from the compiler. I followed the advice of the first answer, and when I loaded my examples, this is what happened:

    prompt$ ghci -fwarn-incomplete-patterns
    
    Prelude> :load myfile.hs 
    [1 of 1] Compiling Main             ( myfile.hs, interpreted )
    
    myfile.hs:3:0:
        Warning: Pattern match(es) are non-exhaustive
                 In the definition of `f': Patterns not matched: [_]
    
    myfile.hs:6:0:
        Warning: Pattern match(es) are non-exhaustive
                 In the definition of `g': Patterns not matched: Nothing
    Ok, modules loaded: Main.
    

    Cool! The compiler is pretty smart!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
i got an object with contents of html markup in it, for example: string
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.