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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:22:57+00:00 2026-05-29T23:22:57+00:00

I written the following code to randomly select items n items from a list

  • 0

I written the following code to randomly select items n items from a list and place the selections in a new list
The code is below

let random_extract list n =
    let l =  ((List.length list)- 1) 
    and acc = []  in
    for i = 1 to n do 
        (List.nth list (Random.int l) ) :: acc  (* Line 447 *)
    done;
    acc
;;

When I load the file that contains this code I get the following error

File "all_code.ml", line 447, characters 2-40:
Warning 10: this expression should have type unit.
val random_extract : 'a list -> int -> 'b list = <fun>

Two questions ,
Question 1: what does this warning mean.

Secondly when I run this function I dont get the list expected but an empty List.

Question 2: how do I get the value of acc from inside the for loop

  • 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-29T23:22:59+00:00Added an answer on May 29, 2026 at 11:22 pm

    What Jeffrey Scofield said, and also:

    let random_extract list n =
    let l =  ((List.length list)- 1) 
    and acc = ref []  in
    for i = 1 to n do 
        acc := (List.nth list (Random.int l) ) :: !acc  (* Line 447 *)
    done;
    !acc
    

    If you are going to use a for loop for something like that, then acc must be a reference, i.e., a mutable value. In Ocaml and ML in general let x = ... defines an immutable value. Next, when you wrote (List.nth list (Random.int l)) :: acc, that just meant “look, I can make a list”. Nowhere did you say that the newly constructed list has to be assigned to anything, in particular it was not assigned to acc (which it couldn’t, because acc is immutable). Ocaml issued a warning because it saw that the body of your for loop produces a list, but Ocaml knows that the body of a for loop ought to produce a unit (“void” in C/C++/Java mis-terminology).

    Using a for loop like that is ugly. The Right Way is to do it like this:

    let random_extract lst =
      let l = List.length lst - 1 in
      let rec extract = function
       | 0 -> []
       | n -> List.nth lst (Random.int l) :: extract (n - 1)
      in
        extract
    

    If you are new to Ocaml you really should invest time to understand the above code completely. After that, you should study the following code, and make sure you understand why it is more efficient that the first version (look up “tail recursion”):

    let random_extract lst =
      let l = List.length lst - 1 in
      let rec extract acc = function
        | 0 -> acc
        | n -> extract (List.nth lst (Random.int l) :: acc) (n - 1)
      in
        extract []
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have written the following code: m_selectCategoryTableWidget = new QTableWidget; m_selectCategoryTableWidget->setRowCount(0); m_selectCategoryTableWidget->setColumnCount(2); m_selectCategoryTableWidget->setHorizontalHeaderLabels(QStringList()<<tr(Category)<<tr(Number of items));
I have written the following IronPython code: import clr clr.AddReference(System.Drawing) from System import *
I've written the following code: for(int layer = 0; layer <countLayers; layer++); { List<Sprite>
I have written the following code as, Dim report As New ReportDocument report.PrintOptions.PrinterName =
i have written the following code var resumeedit=(from t in db.Resumes where t.User.UserID==theUserID &&
I've written following code to get the content from a web page and save
i have written the following code to download file. java.io.BufferedInputStream in = new java.io.BufferedInputStream(new
I have written following code to get JSON result from webservice. function SaveUploadedDataInDB(fileName) {
I have written a following code to get just the file name without extension
I have written the following code choice /m Do you want to add another

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.