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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:58:52+00:00 2026-05-20T18:58:52+00:00

First off, I apologize if this is a confusing or backwards way to go

  • 0

First off, I apologize if this is a confusing or backwards way to go about what I want to accomplish, but I’m new to "OCaml style".

I want to take the last element of a list, and move it to the front of the list, shifting all the elements up one.

For example: have [1;2;3;4;5] -> [5;1;2;3;4]

I understand that lists in OCaml are basically linked list, so I plan to recursively iterate through the list, find the last element, and then have that element’s tail/remaining list point to the head of the list.

What I’m mainly confused about is how to break the link from the second last element to the last element. In the example above, I want to have the 5 point to the 1, but the 4 to no longer point to the 5.

How do I accomplish this, and is there a simpler way to look at this that I’m completely missing?

  • 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-20T18:58:53+00:00Added an answer on May 20, 2026 at 6:58 pm

    You can’t “Break the link” because Ocaml lists are a persistent data-structure. You can’t really modify the lists, so you have to produce a new list with the values in the order you want.

    let thelist = [1;2;3;4;5] in
    let lnewhead = List.hd (List.rev thelist) in
    lnewhead :: (List.rev (List.tl (List.rev b)));;
    

    You could also define this in a function:

    let flipper = fun thelist -> 
        (List.hd (List.rev thelist)) :: (List.rev (List.tl (List.rev thelist)));;
    
    val flipper : 'a list -> 'a list = <fun>
    # flipper([1;2;3;4;5]);;
    - : int list = [5; 1; 2; 3; 4]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off, I apologize if this doesn't make sense. I'm new to XHTML, CSS
First off I apologize... I have posted this question before, but I did a
First off, let me apologize if this has been asked already, but I can’t
First off I would like to apologize beforehand, in case this turns out to
First off, I am fairly new to MVC and jQuery. I apologize if my
First off, apologies if this question has been asked before but I couldn't find
First off, there's a bit of background to this issue available on my blog:
First off, this question is ripped out from this question. I did it because
First off, I know next to nothing about language theory, and I barely know
First off let me apologize to the SO community for coming to you with

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.