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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:53:19+00:00 2026-06-13T22:53:19+00:00

I’m writing an interactive calculator in OCaml with some simple commands. Users should be

  • 0

I’m writing an interactive calculator in OCaml with some simple commands. Users should be able, among other things, to define their own simple functions (mathematical functions), for instance

let f(x) = x
let g(x) = 2*f(x)

Now, the functions should be handled like in functional languages, that means they should remember their time-of-creation environment. That means, that with a function I have to keep a closure of its environment, which is functions and variables.

I keep currently defined functions in a list of tuples formed like (functions_present_at_the_time_of_creation, variables_present_at_the_time_of_creation, function_name, function_argument_names, function_formula). When I try to add a new function to the list of functions (let’s assume, that it’s not currently defined and I don’t have to overwrite anything), I recurrently iterate to the end of the list of functions and there would like to add a new tuple.

The problem is, assuming my current functions list is of type (a*b*c*d*e) list when i try to add a tuple with itself to the end of it, it changes its type to ((a*b*c*d*e) list*f*g*h*i) list. What can I do to be able to perform such addition of a list to itself, encapsulated in a tuple?

Here’s some simple SSCCE I wrote while trying to find a workaround to this issue.

let rec add_to_end list list_copy dummy = match list with
| [] -> [(list_copy, dummy)]
| h::t -> h::(add_to_end t list_copy dummy) 

let add list dummy = add_to_end list list dummy

This one tries to do it with a copy of the list. The following one is written without using of a copy (both of these examples don’t work, of course):

let rec add_to_end list dummy = match list with
| [] -> [(list, dummy)]
| h::t -> h::(add_to_end t dummy) 

The first example doesn’t work when trying to use the function add, but when doing it for instance this way (in the interpreter):

let l = [];;
let l = add_to_end l l 1;;
let l = add_to_end l l 2;;
let l = add_to_end l l 3;;

Then it works fine. I’d appreciate any help, I may think about changing the design also, any proposals are very welcome.

Edit: Here’s the output of the above commands:

# let l = [];;
val l : 'a list = []
# let l = add_to_end l l 1;;
val l : ('a list * int) list = [([], 1)]
# let l = add_to_end l l 2;;
val l : (('a list * int) list * int) list = [([], 1); ([([], 1)], 2)]
# let l = add_to_end l l 3;;
val l : ((('a list * int) list * int) list * int) list =
[([], 1); ([([], 1)], 2); ([([], 1); ([([], 1)], 2)], 3)]
  • 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-06-13T22:53:19+00:00Added an answer on June 13, 2026 at 10:53 pm

    It’s hard to tell whether you’re aware that OCaml lists are immutable. You can’t add a value to the end of an existing list. An existing list can never be changed. You can create a new list with a value added to the end. If you do this, I don’t see why you would want to add a pair to the end consisting of the list and the new value. I suspect you’re thinking about it wrong. Here’s a function that takes a list and an integer and adds the integer to the end of the list.

    # let rec addi i list =
        match list with
        | [] -> [i]
        | h :: t -> h :: addi i t
      ;;
    val addi : 'a -> 'a list -> 'a list = <fun>
    # let x = [1;2;3];;
    val x : int list = [1; 2; 3]
    # addi 4 x;;
    - : int list = [1; 2; 3; 4]
    # x;;
    - : int list = [1; 2; 3]
    # 
    

    The function returns a new list with the value added to the end. The original list isn’t changed.

    As a side comment, it’s much more idiomatic to add values to the front of a list. Repeatedly adding to the end of the list is slow–it gives quadratic behavior. If you want the other order, the usual thing to do is add everything to the front and then reverse the list–this is still linear.

    Edit

    Apparently you really want a function that looks something like this:

    let f a list = list @ [(list, a)]

    This is not realistically possible, the types don’t work out right. A list can contain things of only one type. So you can conclude that the type of the list t is the same as the type (t, v) list, where v is the type of a. This is a recursive type, not something you would really want to be working with (IMHO).

    You can actually get this type in OCaml using -rectypes:

    $ ocaml -rectypes
            OCaml version 4.00.0
    
    # let f a list = list @ [(list, a)];;
    val f : 'a -> (('b * 'a as 'c) list as 'b) -> 'c list = <fun>
    # 
    

    But (as I say) it’s something I would avoid.

    Edit 2

    Now that I look at it, your first code sample avoids requiring a recursive type because you
    specify two different copies of the list. Until you call the function with the same list, these are potentially different types. So the function type is not recursive. When you call with two copies of the same list, you create a new value with a type that’s different than the type of the list. It only works because you’re using the same name l for different values (with different types). It won’t work in a real program, where you’d need a single type representing your list.

    As another side comment: the beauty of adding values to the beginning of a list is that the old value of the list is still there. It’s the tail of the new list. This seems lot closer to what you might actually want to do.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
Seemingly simple, but I cannot find anything relevant on the web. What is the
I am writing an app with both english and french support. The app requests
I'm making a simple page using Google Maps API 3. My first. One marker

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.