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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:28:02+00:00 2026-05-29T13:28:02+00:00

I have an (int * string) tuple which I want to covert into a

  • 0

I have an (int * string) tuple which I want to covert into a list. The tuple is of the form (N, E) where is N is the number of occurances of element E.
The function should return a list with N occurences of E in it. Example is below. Lets assume this function is called tuple_decode.

tuple_decode (1, "A") -> ["A"]
tuple_decode (2,"B") -> ["B";"B"]
tuple_decode (4,"C") - > ["C";"C";"C";"C"]

The tuple_decode function is as follows

let tuple_decode acc (n,elem)   = 
let add_one_elem i = 
    match i  with
           0 -> acc
         | i -> elem :: acc ; add_one_elem (i-1)  (* Line 184 *)
in
add_one_elem n 
;;

when I try to compile this function I get the following error.

File "all_code.ml", line 184, characters 11-22:
Warning 10: this expression should have type unit.
File "all_code.ml", line 184, characters 25-37:
Error: Unbound value add_one_elem

Could someone help me figure out why I get this error and warning.

Regards
Puneet

  • 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-29T13:28:03+00:00Added an answer on May 29, 2026 at 1:28 pm

    The warning comes from sequence composition using ;. When you write S1 ; S2, the compiler expects S1 to have unit type. But here S1 returns a list (elem::acc) whose value will be thrown away. Moreover, since you didn’t pass acc as an argument, its value doesn’t change after all recursive calls.

    The error is due to recursive use of add_one_elem. Because you didn’t use rec keyword, when add_one_elem (i-1) is called, OCaml doesn’t know add_one_elem is being recursively defined.

    Moreover, acc should be a parameter of add_one_elem to accumulate results:

    let tuple_decode (n, elem) = 
      let rec add_one_elem i acc = 
          match i  with
          | 0 -> acc
          | i -> add_one_elem (i-1) (elem::acc)
      in add_one_elem n [] 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a List of tuples of the form (string, int) . I'm trying
how to convert a String type to a Int i have a tuple and
I have the following function: public static byte[] StringToByte(string str) { int length =
If i have a stl map from string to int and i want to
boost::tuple has a get() member function used like this: tuple<int, string, string> t(5, foo,
I have a List of tuple <Tuple<int, int>> x in that I am getting
I am using EF 4 and have the following code. public static Dictionary<Tuple<int, string>,
I have a class Document which consists of Id (int) and Url (String) .
I have a string that looks like L\4\ and want to parse it into
I have tables like this: tblUsers int UserID string UserName tblUsersInRoles int UserID int

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.