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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:36:00+00:00 2026-05-17T20:36:00+00:00

As part of a bigger problem of enumerating a set, I need to write

  • 0

As part of a bigger problem of enumerating a set, I need to write an OCaml function ‘choose’ which takes a list and outputs as the list of all possible sequences of size k made up of elements of that list (without repeating sequences which can be obtained from each other by permutation). The order they are put in the end list is not relevant.

For example,

choose 2 [1;2;3;4] = [[1;2];[1;3];[1;4];[2;3];[2;4];[3;4]]

Any ideas?

I would like to have the whole thing to be lazy, outputting a lazy list, but if you have a strict solution, that’ll be very useful too.

  • 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-17T20:36:01+00:00Added an answer on May 17, 2026 at 8:36 pm

    Here is a strict and suboptimal version. I hope it is clear. It avoids duplicates by assuming there are no duplicates in the input list, and by generating only sublists that are in the same order as in the original list.

    The length computation could be factored by passing l‘s length as an argument of choose. That would make the code less readable but more efficient.

    For the lazy version, sprinkle “lazy” and “Lazy.force” on the code…

    let rec choose k l =
      if k = 0 
      then [ [] ]
      else
        let len = List.length l in
        if len < k
        then []
        else if k = len
        then [ l ]
        else
          match l with
          h :: t ->
              let starting_with_h =
                (List.map (fun sublist -> h :: sublist) (choose (pred k) t))
              in
              let not_starting_with_h = choose k t in
              starting_with_h @ not_starting_with_h
          | [] -> assert false
    ;;
      val choose : int -> 'a list -> 'a list list = <fun>
    
    # choose 3 [1; 2; 3; 4; 5; 6; 7] ;;                        
    - : int list list =
    [[1; 2; 3]; [1; 2; 4]; [1; 2; 5]; [1; 2; 6]; [1; 2; 7]; [1; 3; 4]; [1; 3; 5];
     [1; 3; 6]; [1; 3; 7]; [1; 4; 5]; [1; 4; 6]; [1; 4; 7]; [1; 5; 6]; [1; 5; 7];
     [1; 6; 7]; [2; 3; 4]; [2; 3; 5]; [2; 3; 6]; [2; 3; 7]; [2; 4; 5]; [2; 4; 6];
     [2; 4; 7]; [2; 5; 6]; [2; 5; 7]; [2; 6; 7]; [3; 4; 5]; [3; 4; 6]; [3; 4; 7];
     [3; 5; 6]; [3; 5; 7]; [3; 6; 7]; [4; 5; 6]; [4; 5; 7]; [4; 6; 7]; [5; 6; 7]]
    

    EDIT:

    A lazy_list_append as appears necessary from the comments below:

    type 'a node_t =             
          | Empty
          | Node of 'a * 'a zlist_t
    and 'a zlist_t = 'a node_t lazy_t
    
    let rec lazy_list_append l1 l2 =
      lazy 
        (match Lazy.force l1 with
          Empty -> Lazy.force l2 
        | Node (h, lt) ->
        Node (h, lazy_list_append lt l2))
    ;;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As a part of an assignment, I need to write two functions: a function
The following is a part of a bigger PHP script which adds or disables
This is part of a bigger problem, but I'm having some problems with this
I'm enhancing a client, which is part of a bigger project. Because of the
I wrote this function in C++ as part of a bigger program: Object Single_list<Object>::pop_front()
this problem is part of a bigger page but I've simplified the code to
This is part of a bigger selection, but I have stripped it down to
I have a few issues with the following php functions (part of a bigger
I've tried to extract just the part of the problem where I'm having trouble,
I have a very specific problem which don't seem to be asked before -

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.