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

  • Home
  • SEARCH
  • 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 8933501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T09:33:18+00:00 2026-06-15T09:33:18+00:00

I am learning Jason Hickey’s Introduction to Objective Caml . Here is an exercise

  • 0

I am learning Jason Hickey’s Introduction to Objective Caml.


Here is an exercise I don’t have any clue

enter image description here
enter image description here


First of all, what does it mean to implement a dictionary as a function? How can I image that?

Do we need any array or something like that? Apparently, we can’t have array in this exercise, because array hasn’t been introduced yet in Chapter 3. But How do I do it without some storage?

So I don’t know how to do it, I wish some hints and guides.

  • 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-15T09:33:20+00:00Added an answer on June 15, 2026 at 9:33 am

    I think the point of this exercise is to get you to use closures. For example, consider the following pair of OCaml functions in a file fun-dict.ml:

    let empty (_ : string) : int = 0
    let add d k v = fun k' -> if k = k' then v else d k'
    

    Then at the OCaml prompt you can do:

    
    # #use "fun-dict.ml";;
    val empty : string -> int = 
    val add : ('a -> 'b) -> 'a -> 'b -> 'a -> 'b = 
    # let d = add empty "foo" 10;;
    val d : string -> int = 
    # d "bar";;  (* Since our dictionary is a function we simply call with a
                      string to look up a value *)
    - : int = 0   (* We never added "bar" so we get 0 *)
    # d "foo";;
    - : int = 10  (* We added "foo" -> 10 *)
    

    In this example the dictionary is a function on a string key to an int value. The empty function is a dictionary that maps all keys to 0. The add function creates a closure which takes one argument, a key. Remember that our definition of a dictionary here is function from key to values so this closure is a dictionary. It checks to see if k' (the closure parameter) is = k where k is the key just added. If it is it returns the new value, otherwise it calls the old dictionary.

    You effectively have a list of closures which are chained not by cons cells by by closing over the next dictionary(function) in the chain).

    Extra exercise, how would you remove a key from this dictionary?


    Edit: What is a closure?

    A closure is a function which references variables (names) from the scope it was created in. So what does that mean?

    Consider our add function. It returns a function

    fun k' -> if k = k' then v else d k
    

    If you only look at that function there are three names that aren't defined, d, k, and v. To figure out what they are we have to look in the enclosing scope, i.e. the scope of add. Where we find

    let add d k v = ...
    

    So even after add has returned a new function that function still references the arguments to add. So a closure is a function which must be closed over by some outer scope in order to be meaningful.

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

Sidebar

Related Questions

I am learning Jason Hickey's Introduction to Objective Caml . Just have a question
I am learning Jason Hickey's Introduction to Objective Caml . Just have a question
I am learning Jason Hickey's Introduction to Objective Caml . After I learned Chapter
I have two users Jason and postgres . Since I just started learning this,
This is a learning exercise in expression trees. I have this working code: class
Learning xml, Can anyone help me? I have following XML code: **<book lang=en>name of
I'm learning WPF with MVVM and for a starter, I viewed Jason Dolinger's video
I have decided (whether it's for better or for worse), to start learning the
I am learning json now and have run into an issue: I am using
I have started learning MVC2. I developed a simple customer search screen with Jquery

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.