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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:06:05+00:00 2026-06-18T04:06:05+00:00

In OCaml, for list, we always do first::rest . it is convenient to get

  • 0

In OCaml, for list, we always do first::rest. it is convenient to get the first element out of a list, or insert an element in front of a list.

But why does OCaml not have rest::last? Without List‘s functions, we can’t easily do getting last element of a list or insert an element to the end of a list.

  • 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-18T04:06:06+00:00Added an answer on June 18, 2026 at 4:06 am

    The list datatype is not a magic builtin, only a regular recursive datatype with some syntactic sugar. You could implement it yourself, using Nil instead of [] and Cons(first,rest) instead of first::rest, in the following way:

    type 'a mylist =
      | Nil
      | Cons of 'a * 'a mylist
    

    I’m not sure if you will see the definition above as an answer to your question, but it really is: when you write first::rest, you’re not calling a function, you’re just using a datatype constructor that builds a new value (in constant time and space).

    This definition is simple and has clear algorithmic properties: lists are immutable, accessing the first element of the list is O(1), accessing the k-th element is O(k), concatenation of two lists li1 and li2 is O(length(li1)), etc. In particular, accessing the last element or adding something at the end of a list li would be O(length(li)); we’re not eager to expose this as a convenient operation because it is costly.

    If you want to add elements at the end of a sequence, lists are not the right data structure. You may want to use a queue (if you follow a first-in, first-out access discipline), a deque, etc. There is a (mutable) Queue structure in the standard library, and the two third-party overlays Core and Batteries have a deque module (persistent in Batteries, mutable in Core).

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

Sidebar

Related Questions

As Ocaml does not accept function overloading, sometimes I have to define some similar
What OCaml libraries are out there that provide lazy list handling? I am looking
I am implementing this hoemwork functionality using Ocaml: Not allowed to use List module
I have a list called list_ds of a custom-defined data structure in my OCaml
I’m trying to implement a tail-recursive list-sorting function in OCaml, and I’ve come up
I'm learning OCaml, and this is my first typed language, so try to be
I have experience with OCaml. You had to write a stub for every function
As a beginner in Ocaml, I have this current working code: ... let ch_in
I have a OCaml library with lots of classes I need some translator to
I'm new to ocaml and tryin to write a continuation passing style function but

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.