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

  • Home
  • SEARCH
  • 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 6980945
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:05:32+00:00 2026-05-27T18:05:32+00:00

I am new in haskell and I have a problem (aka homework). So, I

  • 0

I am new in haskell and I have a problem (aka homework).

So, I have a list with a tuple – a string and an integer:

xxs :: [([Char], Integer)] 

I need to know how many of the strings in xxs start with a given character.
Let me exemplify:

foo 'A' [("Abc",12),("Axx",34),("Zab",56)]
Output: 2 

foo 'B' [("Abc",12),("Bxx",34),("Zab",56)]
Output: 1

My best attempt so far:

foo c xxs = length (foldl (\acc (x:xs) -> if x == c then c else x) [] xxs)

But, of course, there’s something VERY wrong inside the lambda expression.

Any suggestion?
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-27T18:05:32+00:00Added an answer on May 27, 2026 at 6:05 pm

    There are a few problems with your attempt:

    1. You plan to use foldl to construct a shorter list and then to take its length. While it is possible, filter function is much better suited for that task as @landei suggests

    2. foldl can be used to accumulate the length without constructing a shorter list. See the answer of @WuXingbo – his answer is incorrect, but once you realize that length is not needed at all with his approach, it should be easy for you to come with correct solution.

    3. Somewhat contradictory to common sense, in a lazy language foldr is faster and uses less memory than foldl. You should ask your teacher why.

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

Sidebar

Related Questions

i'm new in Haskell. I have to write function that takes list of Integers
I am new to Haskell and I have the following problem. I have to
New to Haskell and have a stumbling block. I'm trying to filter a list
I am pretty new to Haskell but I feel like I have a decent
I'm kind of new in Haskell and I have difficulty understanding how inferred types
I'm brand new to Haskell and have very little idea what I'm doing. I've
I'm new to Haskell, so am sorry if this is incredibly obvious... I have
I'm new to Haskell, and I'm trying a bit: isPrime :: Integer->Bool isPrime x
I'm fairly new to Haskell and have a question about pattern-matching. Here is a
I'm very new to Haskell, and I have a simple question. What function can

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.