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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:35:44+00:00 2026-05-25T22:35:44+00:00

Is it really true that OCaml doesn’t have a function which converts from a

  • 0

Is it really true that OCaml doesn’t have a function which converts from a list to a set?

If that is the case, is it possible to make a generic function list_to_set? I’ve tried to make a polymorphic set without luck.

  • 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-25T22:35:45+00:00Added an answer on May 25, 2026 at 10:35 pm

    Fundamental problem: Lists can contain elements of any types. Sets (assuming you mean the Set module of the standard library), in contrary, rely on a element comparison operation to remain balanced trees. You cannot hope to convert a t list to a set if you don’t have a comparison operation on t.

    Practical problem: the Set module of the standard library is functorized: it takes as input a module representing your element type and its comparison operation, and produces as output a module representing the set. Making this work with the simple parametric polymoprhism of lists is a bit sport.

    To do this, the easiest way is to wrap your set_of_list function in a functor, so that it is itself parametrized by a comparison function.

    module SetOfList (E : Set.OrderedType) = struct
      module S = Set.Make(E)
      let set_of_list li =
        List.fold_left (fun set elem -> S.add elem set) S.empty li
    end
    

    You can then use for example with the String module, which provides a suitable compare function.

      module SoL = SetOfList(String);;
      SoL.S.cardinal (SoL.set_of_list ["foo"; "bar"; "baz"]);; (* returns 3 *)
    

    It is also possible to use different implementation of sets which are non-functorized, such as Batteries and Extlib ‘PSet’ implementation (documentation). The functorized design is advised because it has better typing guarantees — you can’t mix sets of the same element type using different comparison operations.

    NB: of course, if you already have a given set module, instantiated form the Set.Make functor, you don’t need all this; but you conversion function won’t be polymorphic. For example assume I have the StringSet module defined in my code:

    module StringSet = Set.Make(String)
    

    Then I can write stringset_of_list easily, using StringSet.add and StringSet.empty:

    let stringset_of_list li =
      List.fold_left (fun set elem -> StringSet.add elem set) StringSet.empty li
    

    In case you’re not familiar with folds, here is a direct, non tail-recursive recursive version:

    let rec stringset_of_list = function
      | [] -> StringSet.empty
      | hd::tl -> StringSet.add hd (stringset_of_list tl)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it really true that Crystal Reports does not support images that have transparency?
Really quick jQuery question... I have this function: $(document).ready(function() { $('a#show1').click(function() { $('.item1').toggle(1000); return
Can it really be true that the attr(href) command for a link is handled
Is it true that a mutex can only be released from the thread that
1) Is it true that if you would like to have your custom dialogues
Really my question has more to do with the server-side scrubbing of html that's
Really ripping my hair out on this one. I have a JAAS Authentication Provider
Really simple question: From within GWT I want to forward the user away from
Possible Duplicate: Encrypting/Hashing plain text passwords in database Recently, I discovered that major web
Is it true that UITabBarController never calls initWithNibName on its child views (individual tabs)?

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.