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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:25:32+00:00 2026-05-14T00:25:32+00:00

I have this but I got an error: — test if a list contains

  • 0

I have this but I got an error:

-- test if a list contains exactly three characters
test :: [Char] -> Bool
test xs   | [_ , _ , _] = True
          | otherwise = False
  • 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-14T00:25:32+00:00Added an answer on May 14, 2026 at 12:25 am

    Pattern matching happens on the left side of the vertical bar. Thus:

    test :: [Char] -> Bool
    test [_, _, _] = True
    test _         = False
    

    As Norman Ramsey rightly notes below, the following code is not a robust alternative solution (because it is very inefficient when applied to (long) finite lists and does not halt on infinite lists) and should thus not be used:

    test :: [Char] -> Bool
    test xs = length xs == 3
    

    Moreover, length xs == 0 should always be replaced with null xs.

    Edit: the question which naturally arises is: how do we generalize this? What if we want to test whether a list has exactly n elements? What if the input may be infinite?
    Here’s a solution where the cost is either n or the length of the list, whichever is smaller—and that’s as efficient a solution, asymptotically, as we can possibly hope for:

    hasLength :: Int -> [a] -> Bool
    hasLength n []     = n == 0
    hasLength 0 (x:xs) = False
    hasLength n (x:xs) = hasLength (n-1) xs
    

    Usage:

    *Main> hasLength 3 [1..2]
    False
    *Main> hasLength 3 [1..3]
    True
    *Main> hasLength 3 [1..4]
    False
    *Main> hasLength 3 [1..] 
    False
    

    It is unsafe to call this function with a negative length; if the list is infinite, the function won’t terminate, and if the list is finite, it will return False with cost proportional to the length of the list. An easy fix would be to check in advance (on the first call only) that n is nonnegative, but this fix would ugly up the code.

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

Sidebar

Related Questions

I have prefer this post for example but I got the error at adding
I have been researching this for a while but got no convinced answer. From
I don't know if I have got this correct but what I am trying
I have a database connection that I got like this: db = Mongo::Connection.new.db(app-development) but
i have this code: public class Test{ arrayList<String> list = new ArrayList<String>(); String[][] temp_list;
I've read a few posts on this but I'm still confused. I have this
i have this query but this shows column does not exist SELECT p.content, (select
i have this application but how to use its? and example for 7-zip can?
I have tried this but it does not work (even if I specify .wav
I have tried this but it doesn't work: tell application Preview set myfile to

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.