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 7024389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:50:00+00:00 2026-05-27T23:50:00+00:00

I am trying to count the elements of a list that are the same

  • 0

I am trying to count the elements of a list that are the same length for n and n+1.

I wrote this code:

countSec :: [[Integer]] -> Integer
countSec (x:xs) = if (length x)==(length (head xs)) 
    then 1+(countSec xs) 
    else (countSec xs)
countSec [] = 0

As you may guess, it’s not working. In fact, as a output I get “* Exception: Prelude.head: empty list”

countSec [[1,2][1,2],[2],[3,5],[2],[5]] should return 2 (the first two and the last two sublists have the same length).

Any clue on what the problem can be?

Thanks

  • 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-27T23:50:01+00:00Added an answer on May 27, 2026 at 11:50 pm

    You don’t handle the case where there is only one element in the list of lists, so you run into an error as soon as you call head on xs, which will be [], since there’s no first element of an empty list 🙂

    You can use pattern matching to fix this:

    countSec :: [[Integer]] -> Integer
    countSec [] = 0
    countSec [_] = 0
    countSec (x : xs@(y:_))
      | length x == length y = 1 + countSec xs
      | otherwise            = countSec xs
    

    I used pattern matching to match two elements of the list at a time. This is just like x:(y:_) (patterns can nest), except that we also give the name xs to the thing we matched to y:_; so we have

                 xs
    x      y _____\_____
     \      \           \
      [1] : ([2, 3] : ...)
    

    x is the first element of the list, y is the second, and xs is the list after the first element. We could just match (x:y:xs) instead and use countSec (y:xs) to recurse, but this would use up more memory and is more error-prone.

    The _ in a pattern means that we don’t care about the value at that position, and so don’t want to give it a name; it’s a wildcard that matches everything.

    As a minor stylistic note, I also converted your if expression into a guard; these are basically just if expressions at the level of a function clause.

    You should avoid using partial1 functions like head when pattern matching would work because of things like this — they hide errors and make code harder to read. I also suggest giving the -Wall flag to GHC; if you wrote countSec as I did but forgot the zero or one-element case, GHC would warn you about it:

    Warning: Pattern match(es) are non-exhaustive
             In an equation for `countSec': Patterns not matched: [_]
    

    1 A function not defined for all its inputs; for instance, head and tail are not defined on [], and division is not defined when the divisor is 0.

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

Sidebar

Related Questions

Trying to count how many elements within the array are not equal to 0,
I'm trying to count the number of records that have a null DateTime value.
I am trying to count characters in comments included in C code using Python
My problem is that I'm trying to count which tag has been used most
I am trying to read in a file that has HTML list items with
I'm analyzing some code that utilizes empty OVER clauses in the contest of Count().
I have this XML file that I parse into its elements and create a
I am trying to write a procedure order(List,Result) that has a List as input
I've been trying to make some methods that will sort a linked list by
I am trying to translate the VBA code found in this link into IronPython.

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.