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

  • Home
  • SEARCH
  • 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 6791357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:50:12+00:00 2026-05-26T17:50:12+00:00

I am trying to create an OCaml function rv that will go through a

  • 0

I am trying to create an OCaml function rv that will go through a list of lists and reorganize the elements into another list of lists such that the first list is comprised of the first element of each of the original lists, the second list is comprised of the second element of each list etc.

An example:
[["a"; "b"; "c"]; ["d"; "e"; "f"]] becomes
[["a"; "d"]; ["b"; "e"]; ["c"; "f"]]

I already have a function gather that will take a list of lists and extract the elements of a certain index from each list. So basically using the previous example I could say gather(list, 1) and get ["b"; "e"] back. I need a way to go through List.length times and use gather for each element index.

The reason this is causing me so much trouble is because of the limitations applied (yes, this is for school). I am not allowed to use loops or local/global variables. I am also not allowed to use ‘let’ within the body of a function. Is there another way to recursively use gather for each element index? Is there another function I should make that will recursively use gather and then re-use that function in rv? If so, how would you go about that?

  • 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-26T17:50:12+00:00Added an answer on May 26, 2026 at 5:50 pm

    To overcome the restriction against loops, use recursive functions.

    The restriction about using let is a little silly. You can work around it by passing arguments to a function, to some extent, writing (function x -> e) v instead of let x = v in e. But think of it as a hint that you’re supposed to leverage functions such as List.map and List.fold_left and so on.

    Generally speaking, lists are not arrays; they’re not meant to be accessed with the index of an element. Sure, you can do it, but it’s cumbersome and inefficient. The natural way to access a list is to use pattern matching or the List.hd and List.tl functions to split out the first element and the remainder of the list, or to use functions such as List.map to traverse the whole list.

    For example, if all you needed was the first element of each list, then you could use

    List.map List.hd lists
    

    And List.map List.tl lists gives the remainder of each list. This suggests a recursive method: the head of the result is List.map List.hd lists, and the tail of the result is the transposition of List.map List.tl lists.

    let rec transpose lists =
      List.map List.hd lists :: transpose (List.map List.tl lists)
    

    Now, obviously, we need to terminate at some point. Assuming all lists have the same length, we need to stop when the lists are empty, which we can test by looking at the first list. I will leave this as an exercise.

    To understand what this code is doing, I recommend typing it in the Ocaml toplevel, using the #trace directive and watching the operation on a few examples. For this, it will help if you make the transpose function monomorphic.

    # let rec tranpose (lists : string list list) = …;;
    val transpose : string list list -> string list list = <fun>
    # #trace transpose;;
    transpose is now traced.
    # transpose [["a"; "b"; "c"]; ["d"; "e"; "f"]];;
    transpose <-- [["a"; "b"; "c"]; ["d"; "e"; "f"]]
    transpose <-- [["b"; "c"]; ["e"; "f"]]
    transpose <-- [["c"]; ["f"]]
    transpose <-- [[]; []]
    transpose --> []
    transpose --> [["c"; "f"]]
    transpose --> [["b"; "e"]; ["c"; "f"]]
    transpose --> [["a"; "d"]; ["b"; "e"]; ["c"; "f"]]
    - : string list list = [["a"; "d"]; ["b"; "e"]; ["c"; "f"]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying create this function such that if any key besides any of the
Trying to create my first iPhone app that would play back audio. When I
Trying to create a select list with a first option text set to an
Hi I am trying create a system call that will count the number of
I'm currently trying create a service that will return results of a OLAP cube
Trying to create a widget in the baseclass and the only thing that will
I am trying create a WCF service that leverages the WPF MediaPlayer on the
Trying to create a small monitor application that displays current internet usage as percentage
Trying to create a list to return some JSON data to a view. Following
I'm a complete newbie in OCaml and trying to create a simple console program.

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.