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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:35:29+00:00 2026-05-28T07:35:29+00:00

I am trying to understand what this block of code is doing: let rec

  • 0

I am trying to understand what this block of code is doing:

let rec size x =
    match x with
      [] -> 0
    | _::tail -> 1 + (size tail) ;;

I know that this expression computes the size of a list, but I don’t understand where in the code it reduces the list one by one. For example, I think it needs to go from [1;2;3] to [2;3] to [3], but where or how does it do it? I don’t get it.

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-28T07:35:30+00:00Added an answer on May 28, 2026 at 7:35 am

    A list in OCaml is built recursively using empty list ([]) and cons (::) constructor. So [1; 2; 3] is a syntactic sugar of 1::2::3::[].

    The size is computed by reducing x in each step using the pattern _::tail (_ denotes that we ignore the head of the list) and calling the same function size on tail. The function eventually terminates when the list is empty and the pattern of [] succeeds.

    Here is a short illustration of how size [1; 2; 3] is computed:

       size 1::2::3::[]
    ~> 1 + size 2::3::[] // match the case of _::tail
    ~> 1 + 1 + size 3::[] // match the case of _::tail
    ~> 1 + 1 + 1 + size [] // match the case of _::tail
    ~> 1 + 1 + 1 + 0 // match the case of []
    ~> 3
    

    As a side note, you can see from the figure that a lot of information needs to be stored in the stack to compute size. That means your function could result in a stack overflow error if the input list is long, but it is another story.

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

Sidebar

Related Questions

I am trying to understand this simple hashlib code in Python that has been
I'm having a weird situation that I'm trying to understand. This piece of code
I'm trying to understand this bit of code: in display.php: <html> ... <body> <table>
I am trying to understand this inline assembly code which comes from _hypercall0 here
I don't know much about database optimization, but I'm trying to understand this case.
I'm trying to understand how this exactly works (I know what it does, I
Trying to understand Ruby a bit better, I ran into this code surfing the
I am trying to understand a code which block some keys on the keyboard
I've inherited a code block that contains the following regex and I'm trying to
I have a block of uncommented code I'm trying to understand. The comments are

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.