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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:44:16+00:00 2026-05-31T18:44:16+00:00

I am new to OCaml, and I am now trying to implement a function

  • 0

I am new to OCaml, and I am now trying to implement a function that returns a list of elements of a given list x at indexes in list y.

For example, that function should perform the following computation: [5,6,7,8], [0, 3] => [5, 8]

I am not sure how to store temp variables in ML and don’t have a clear idea how it works. I do know how to find a element from a list given specified index, though.

Any idea will be appreciated, but I’d like to use recursive functions and avoid the List module.

  • 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-31T18:44:18+00:00Added an answer on May 31, 2026 at 6:44 pm

    No need for temporary variables, just use recursion!

    # let rec indices xs = function
        | i :: is -> (List.nth xs i) :: indices xs is
        | [] -> []
      ;;
    val indices : 'a list -> int list -> 'a list = <fun>
    
    # indices [5;6;7;8] [0;3] ;;
    - int list = [5; 8]
    

    It builds up the list by going through each of the indexes provided and then consing that onto the list returned by the next step.

    Hopefully this is also optimised into a tail recursive form, but I’m not so sure about that. You may want to change it to be properly tail-recursive, but I’ll leave that up to you.

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

Sidebar

Related Questions

I'm trying to write a simple recursive function that look over list and return
I'm new to OCaml and attempting to implement List.append as a learning exercise. This
I'm new to ocaml and tryin to write a continuation passing style function but
I'm pretty new to OCaml and was trying to figure out how to call
I'm currently learning OCaml, and it's giving me some problems. I'm trying to implement
I am new to Ocaml, just want to make sure how to perform a
New to both Ruby and Rails but I'm book educated by now (which apparently
New to Linux programming in general. I am trying to communicate with a kernel
I'm new at OCaml (and still a novice in learning programming in general) and
I'm new to Ocaml. The problem I'm having is keeping track of the number

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.