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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:12:46+00:00 2026-05-15T00:12:46+00:00

In Programming F# I came across a pattern-matching like this one (I simplified a

  • 0

In “Programming F#” I came across a pattern-matching like this one (I simplified a bit):

let rec len list = 
  match list with
  | [] -> 0
  | [_] -> 1
  | head :: tail -> 1 + len tail;;

Practically, I understand that the last match recognizes the head and tail of the list. Conceptually, I don’t get why it works. As far as I understand, :: is the cons operator, which appends a value in head position of a list, but it doesn’t look to me like it is being used as an operator here. Should I understand this as a “special syntax” for lists, where :: is interpreted as an operator or a “match pattern” depending on context? Or can the same idea be extended for types other than lists, with other operators?

  • 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-15T00:12:47+00:00Added an answer on May 15, 2026 at 12:12 am

    In addition to Brian’s answer, there are a few points that are worth noting. The h::t syntax can be used both as an operator and as a pattern:

    let l = 1::2::[]                    // As an operator
    match l with x::xs -> 1 | [] -> 0   // As a pattern
    

    This means that it is a bit special construct, because other operators (for example +) cannot be used as patterns (for decomposing the result back to the arguments of the operator) – obviously, for +, this would be ambiguous.

    Also, the pattern [_] is interesting, because it is an example of nested pattern. It composes:

    • _ – Underscore pattern, which matches any value and doesn’t bind any symbols
    • [ <pattern> ] – Single-element list pattern, which matches a lists with single elements and matches the element of the list with the nested <pattern>.

    You could also write match 1::[] with | [x] -> x which would return the value of the single element (in this case 1).

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

Sidebar

Related Questions

I came across this statement as code, on a programming puzzle web site. (i.
I came across this code in Stephen G Kochan's book, Programming in c. Is
I was fooling around with some functional programming when I came across the need
I've been reading various programming styles like XP (writing the test first), and came
I recently came across Erlang, the programming language, and I've become interested in developing
While reading SICP I came across logic programming chapter 4.4. Then I started looking
I'm doing code review and came across a class that uses all static methods.
I first came across exceptions with ADA 83. As far as I know, the
Programming Student here...trying to work on a project but I'm stuck. The project is
Programming in vim I often go search for something, yank it, then go back

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.