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
Please find my code below. My problem is that I the inner jQuery.get() doesn't
I was wondering if someone could work this one out as I've been trying
Please note that Object.Watch and Object.Observe are both deprecated now (as of Jun 2018).
I am writing a little utility function in Python which returns a boolean, indicating
I am attempting to launch a specific form depending on the selected node of
F# is giving me some trouble with its type inference rules. I'm writing a
I was trying to export a simple test function for a dll to work
$('#nav a').click(function () { $('#nav a').removeClass('current'); $(this).addClass('current'); return false; }); My HTML is: <ul
i need to test if any of the strings 'hello', 'i am', 'dumb' exist

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.