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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:17:17+00:00 2026-05-13T00:17:17+00:00

I wanted to have a tail-recursive version of List.map , so I wrote my

  • 0

I wanted to have a tail-recursive version of List.map, so I wrote my own. Here it is:

let rec list_map f l ?(accum=[])=
  match l with
      head :: tail -> list_map f tail ~accum:(head :: accum)
    | [] -> accum;;

Whenever I compile this function, I get:

File "main.ml", line 69, characters 29-31:
Warning X: this optional argument cannot be erased.

The tutorial says that this means that I’m trying to create a function with no non-optional arguments. But the function above clearly takes non-optional arguments.

I’m probably just doing something really dumb, but what?

  • 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-13T00:17:17+00:00Added an answer on May 13, 2026 at 12:17 am

    The previous solutions do compile, but won’t give the expected result. The function f is never applied to the arguments. A correct code is:

    let rec list_map f ?(accum = []) l = match l with
        | head :: tail -> list_map f ~accum:(f head :: accum) tail
        | [] -> accum;;
    

    The inferred type is:

    val list_map : ('a -> 'b) -> ?accum:'b list -> 'a list -> 'b list = <fun>
    

    … in contrast to the wrong one:

    val list_map : 'a -> ?accum:'b list -> 'b list -> 'b list = <fun>
    

    Please note, that the result list is reversed:

    # list_map ( ( ** ) 2.) [1.;2.;3.;4.];;
    - : float list = [16.; 8.; 4.; 2.]
    

    … and equals the function rev_list from the List module:

    # List.rev_map ( ( ** ) 2.) [1.;2.;3.;4.];;
    - : float list = [16.; 8.; 4.; 2.]
    

    So you may want to change your function into:

    let rec list_map f ?(accum = []) l = match l with
        | head :: tail -> list_map f ~accum:(f head :: accum) tail
        | [] -> List.rev accum;;
    

    … which should be tail-recursive as well (according to the manual) and returns the list in the original order:

    # list_map ( ( ** ) 2.) [1.;2.;3.;4.];;
    - : float list = [2.; 4.; 8.; 16.]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I wanted to have an application that could easily switch the DB
Say I wanted to have a project, and one-to-many with to-do items, and wanted
If I wanted to have Python distributed across multiple processors on multiple computers, what
Ruby is preinstalled on my Mac and so I wanted to have a look
I've been working on a project in Delphi 7 where I wanted to have
Iam a great fan of javascript frameworks especially jQuery .I have always wanted to
I have a quick little application and wanted to give a try to develop
I have a button which I wanted to flash briefly to get the user's
I have a ListView inside of an Update Panel and wanted to change the
I have a block of code that works and I wanted to ask what

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.