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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:16:24+00:00 2026-05-21T18:16:24+00:00

I have always been challenged by the way lists work and am confused about

  • 0

I have always been challenged by the way lists work and am confused about the whole (x:xs) concept. I just don’t seem to be getting my head around it.

Example

select :: Ord a => a -> [a] -> [a]
select y [] = []
select y (x:xs)
    | x < y     = x : select y xs
    | otherwise = select y xs

P.S. I know exactly what the function does, but would anyone be able to explain the process (including especially the weird Ord a and => signs)?

Any effective strategies would be much appreciated.

Thanks in advance. Ian.

  • 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-21T18:16:25+00:00Added an answer on May 21, 2026 at 6:16 pm

    Ok. Let’s walk through the different syntactic elements here.

    Line 1

    select :: Ord a => a -> [a] -> [a]
    

    This is a type declaration. It is the declaration of a function (since it has -> types).

    The function has two arguments.

    • The first is a single value of any type, signified by a (lower case means it is a polymorphic type).
    • The second argument is a list of any type that is the same as the first argument.

    The return value is a list of any type, the same as the argument types.

    The Ord a component is a typeclass constraint, that says that any type this function is given must also be an instance of the Ord class. This is a class of types that can be compared.

    Line 2

    Now we look at line 2:

    select y [] = []
    

    This is one definition of the select function itself. It is very simple, containing patterns for the two arguments, and a specification of the result. It reads:

    if the first argument is any value (which we will name y), and the second argument is the empty list (denoted by the [] pattern), then select evaluates to the empty list.

    Line 3

    Line 3 contains the other case for lists:

    select y (x:xs)
    

    Again, this is part of the definition of the select function, for the case when the second argument is not an empty list. If it is not an empty list, then it is a list with a head, x, and a tail xs. The “cons” constructor, (:) combines a list head and tail. It is also how we pattern match on a list, to extract the head and tail.

    By pattern matching on the head and tail of the list, with (x:xs), we bind a new variable, x, to the value of the head of the list, and xs to value of the tail of the list.

    Lines 4 and 5

    The last two lines are additional guards that test and branch based on additional checks, should the second argument be a non-empty list:

    | x < y     = x : select y xs
    | otherwise = select y xs
    

    The first guard fires when x is less than the first argument, y. If so, we return a new list with x at the head, and select applied again to the tail.

    If that is not the case, then we drop x from the list, and return only what happens when tail is called recursively.


    For more information on how Haskell works, I recommend introductory texts such as:

    • Learn You a Haskell for Great Good.
    • Programming in Haskell.
    • The Craft of Functional Programming.

    it will be worth your time.

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

Sidebar

Related Questions

I have been searching for days for a way to always display just one
I have always been kind of confused by threads, and my class right now
I have always been confused that .e,g in php i have sql statement $qry
I have always been curious about this - why do in C++ I have
At the company I work for date and time values have always been stored
I have always been a client server developer. I am getting into mobile development.
I have always been slightly confused with the amount of different IO implementations in
I'm a solo developer and always have been, but I've been learning lately about
I have a question about Subversion. I have always been an end user so
I have always been wondering about why in the following example it is OK

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.