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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:02:52+00:00 2026-06-17T16:02:52+00:00

I wish to undestand an example from book cited below. I understand that this

  • 0

I wish to undestand an example from book cited below. I understand that this is something about pattern matching, but where can I find full and exact description of what match expression means?

# let rec sort lst =
# match lst with
# [] -> []
# | head :: tail -> insert head (sort tail)
# and insert elt lst =
# match lst with
# [] -> [elt]
# | head :: tail -> if elt <= head then elt :: lst else head :: insert elt tail
# ;;
  • 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-06-17T16:02:53+00:00Added an answer on June 17, 2026 at 4:02 pm

    This page in the OCaml manual explains how match works:

    The expression

    match expr 
    with  pattern1    ->  expr1 
    | … 
    | patternn    ->  exprn
    

    matches the value of expr against the patterns pattern1 to patternn.
    If the matching against patterni succeeds, the associated expression
    expri is evaluated, and its value becomes the value of the whole match
    expression. The evaluation of expri takes place in an environment
    enriched by the bindings performed during matching. If several
    patterns match the value of expr, the one that occurs first in the
    match expression is selected. If none of the patterns match the value
    of expr, the exception Match_failure is raised.

    That is, it tries one pattern after the other until it finds one that matches. Once it does, it returns the expression associated with that pattern (i.e. the expression on the right side of the ->). If no pattern matches, you get an exception.

    Which kinds of patterns there are and what they mean is explained on this page of the manual. That’s a bit much though, so here’s a summary of the relevant bits:

    The most important patterns are variable patterns and variant patterns:

    A variable pattern is simply a variable name. This pattern always matches and allows you to refer to the matched expression by the given name on the right side of the ->. Instead of a name you can also use _, which also always matches, but doesn’t allow you to refer to the value on the right side of the ->.

    A variant pattern is the name of a constructor of a variant type followed by as many patterns as the constructor takes arguments. This pattern matches if the value you’re matching against is using that specific constructor and if each of the elements inside that value match the corresponding patterns.

    In your example, the first pattern is []. This is the constructor of the list type that represents empty lists. The [] constructors takes no arguments. So this pattern matches if the list is empty.

    The second pattern is head :: tail. :: is the constructor of the list type that represents non-empty lists. The :: constructor takes two arguments: the head of the list and the tail of the list. head and tail are variable patterns that are matches against those two arguments of the :: constructor. So this pattern matches if the list is non-empty and assigns the variables head and tail to the head and the tail of the non-empty list respectively.

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

Sidebar

Related Questions

I understand the theory of how JSONP works, but something about the particulars or
This is something that has always bothered me about PHP and I have never
I wish to understand more about parameter parsing. I have an example with the
Really wish i was God, then I would understand what this means: The element
I wish HTML could do something semantically equivalent to this; <dl class=main-list> <definitionitem> <dt>Some
I wish best understand the difference between dealloc and release function.... example... I have
I'm not new to JavaScript, but I never could understand a certain thing about
I wish to do this using only XAML - How to add an additional
I wish I could figure this out. I need to produce a table with
I wish to iterate over a set but the contents of the set will

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.