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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:41:47+00:00 2026-05-14T14:41:47+00:00

Could someone please show me why the function below expects integer[] instead of byte[]

  • 0

Could someone please show me why the function below expects integer[] instead of byte[]

type Node =
    | InternalNode of int*Node*Node
    | LeafNode of int * byte

let weight node =
    match node with 
    |InternalNode(w,_,_) -> w
    |LeafNode(w,_)-> w

let createNodes inputValues =
    let getCounts (leafNodes:(int*byte)[])= 
        inputValues |>Array.iter
            (fun b-> let (w,v) =leafNodes.[(int)b]
                     leafNodes.[(int)b]<-(w+1,v))

        leafNodes
    [|for b in 0uy..255uy -> (0 ,b)|] |>getCounts
    |>List.ofArray
    |>List.map LeafNode
  • 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-14T14:41:48+00:00Added an answer on May 14, 2026 at 2:41 pm

    The only place that tells the F# compiler something about the type of the parameter is inside the lambda function given to Array.iter (from the use of this higher-order function, the compiler infers that you’re working with arrays). Inside the lambda function you have:

    leafNodes.[(int)b] 
    

    As a side-note, int in this code is just a normal F# function (not a special type cast construct), so the usual way to write it would be just:

    leafNodes.[int b] 
    

    Now, the compiler knows that b (that is, values of the array given as the argument) can be converted to integer, however the int function works with other types (you can write for example int 3.13f. In ambiguous cases like this, the compiler uses int as the default type, so that’s the reason why you’re seeing a type int[].

    You can add type annotations to the declaration like this (and it will work without any other changes, because byte can be converted to integer using the int function):

    let createNodes (inputValues:byte[]) =  
      // ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Could someone please show me where to go for some decent tutorials for setting
Could someone please demystify interfaces for me or point me to some good examples?
Could someone please tell me which objects types can be tested using Regular Expressions
Could someone please explain the best way to connect to an Interbase 7.1 database
Could someone please point me toward a cleaner method to generate a random enum
Could someone please explain? I couldn't find anything on the internet, everything talks about
I've recently written this with help from SO. Now could someone please tell me
Could someone simply explain the 3 phases in the Flash Event framework, please? By
Could someone tell me what the units the SetTimeout(int) method in the ICriteria interface
Could someone recommend any good resources for creating Graphics User Interfaces, preferably in C/C++?

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.