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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T01:58:13+00:00 2026-06-02T01:58:13+00:00

I’m reading Expert F# book and I found this code open System.Collections.Generic let divideIntoEquivalenceClasses

  • 0

I’m reading Expert F# book and I found this code

open System.Collections.Generic
let divideIntoEquivalenceClasses keyf seq =
// The dictionary to hold the equivalence classes
  let dict = new Dictionary<'key,ResizeArray<'T>>()
  // Build the groupings
  seq |> Seq.iter (fun v ->
                          let key = keyf v
                          let ok,prev = dict.TryGetValue(key)
                          if ok then prev.Add(v)
                          else let prev = new ResizeArray<'T>()
                             dict.[key] <- prev
                             prev.Add(v))

 dict |> Seq.map (fun group -> group.Key, Seq.readonly group.Value)

and the example use:

> divideIntoEquivalenceClasses (fun n -> n % 3) [ 0 .. 10 ];;
val it : seq<int * seq<int>>
= seq [(0, seq [0; 3; 6; 9]); (1, seq [1; 4; 7; 10]); (2, seq [2; 5; 8])]

first for me this code is really ugly, even if this is safe, It looks more similar to imperative languages than to functional lang..specially compared to clojure. But the problem is not this…I’m having problems with the Dictionary definition

when I type this:

let dict = new Dictionary<'key,ResizeArray<'T>>();;

I get this:

pruebafs2a.fs(32,5): error FS0030: Value restriction. The value 'dict' has been inferred to have generic type
val dict : Dictionary<'_key,ResizeArray<'_T>> when '_key : equality    

Either define 'dict' as a simple data term, make it a function with explicit arguments or, if you do not intend for it to be generic, add a type annotation.

is It ok?…

thanks so much


improve question:

Ok I’ve been reading about value restriction and I found this helpfull information

In particular, only function definitions and simple immutable data
expressions are automatically generalized

…ok..this explains why

let dict = new Dictionary<'key,ResizeArray<'T>>();;

doesn’t work…and show 4 different techniques, although in my opinion they only resolve the error but aren’t solutions for use generic code:

Technique 1: Constrain Values to Be Nongeneric

 let empties : int list [] = Array.create 100 []

Technique 3: Add Dummy Arguments to Generic Functions When Necessary

let empties () = Array.create 100 []
let intEmpties : int list [] = empties()   

Technique 4: Add Explicit Type Arguments When Necessary (similar to tec 3)

let emptyLists = Seq.init 100 (fun _ -> [])
> emptyLists<int>;;
val it : seq<int list> = seq [[]; []; []; []; ...]

—– and the only one than let me use real generic code ——
Technique 2: Ensure Generic Functions Have Explicit Arguments

let mapFirst = List.map fst //doesn't work
let mapFirst inp = List.map fst inp

Ok, in 3 of 4 techniques I need resolve the generic code before can work with this…now…returning to book example…when the compile knows the value for ‘key and ‘T

let dict = new Dictionary<‘key,ResizeArray<‘T>>()

in the scope the code is very generic for let key be any type, the same happen with ‘T

and the biggest dummy question is :

when I enclose the code in a function (technique 3):

let empties = Array.create 100 [] //doesn't work
let empties () = Array.create 100 []
val empties : unit -> 'a list []

I need define the type before begin use it
let intEmpties : int list [] = empties() 

for me (admittedly I’m a little dummy with static type languages) this is not real generic because it can’t infer the type when I use it, I need define the type and then pass values (not define its type based in the passed values) exist other way define type without be so explicit..

thanks so much..really appreciate any help

  • 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-02T01:58:14+00:00Added an answer on June 2, 2026 at 1:58 am

    This line

    let dict = new Dictionary<'key,ResizeArray<'T>>();;
    

    fails because when you type the ;; the compiler doesn’t know what 'key and 'T are. As the error message states you need to add a type annotation, or allow the compiler to infer the type by using it later or make it a function

    Examples

    Type annotation change

    let dict = new Dictionary<int,ResizeArray<int>>();;
    

    Using types later

    let dict = new Dictionary<'key,ResizeArray<'T>>()
    dict.[1] <- 2
    

    using a function

    let dict() = new Dictionary<'key,ResizeArray<'T>>();;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
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
this is what i have right now Drawing an RSS feed into the php,
Does anyone know how can I replace this 2 symbol below from the string
I'm working with an upstream system that sometimes sends me text destined for HTML/XML

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.