F# undefined type error when annotating variable


Quantum

I have a function with a recursive function that counts the letters in a word to "hello"return [|("h",1);("e",1);("l",2);("o",1);|]. My code is as follows:

let letterCheck (a:string) =
    let aList = a |> Seq.toList;

    let rec _check (charList:char list) (result:(char * int) array) = 
        match charList with
        | head :: tail -> 
            if Array.exists (fun (c,i) -> c = head) result then
                let index = Array.findIndex (fun (c,i) -> if c = head then true else false) result;
                Array.set result index (result.[index].[1]+1);
            else
                Array.append result [|(head,1)|];
            _check tail result;
        | [] -> result;
    _check aList [||];

However, even if I enter the annotated result, it throws Array.setan error in the error:

Program.fs(73,45): error FS0752: The operator 'expr has been placed according to previous information for this program. [idx]' for objects of indeterminate type. Consider adding other type constraints.

How can I fix this error, or am I doing something fundamentally wrong?

Taylor Wood

Am I doing something fundamentally wrong?

This code looks like it expects mutable values:

        if Array.exists (fun (c,i) -> c = head) result then
            let index = Array.findIndex (fun (c,i) -> if c = head then true else false) result;
            Array.set result index (result.[index].[1]+1);
        else
            // new array value will be discarded
            Array.append result [|(head,1)|];
        _check tail result; // result will be unchanged

Array.appendReturns the modified array value; it does not change the value of the input array. result.[index].[1]+1It looks like you're trying to get the second item from the tuple using the array index syntax ( use sndinstead ) , but even if the function is fixed, there's still a problem because it will always recurse with the same/unchanged value result.

Also, you don't need to end every line with a semicolon in F#.

Here's a version of your function that can be used with minimal changes and no extra type annotations:

let letterCheck (a:string) =
    let aList = a |> Seq.toList
    let rec _check charList result = 
        match charList with
        | head :: tail -> 
            if Array.exists (fun (c,i) -> c = head) result then
                let index = Array.findIndex (fun (c,i) -> c = head) result
                Array.set result index (head, (snd(result.[index])+1))
                _check tail result
            else
                _check tail (Array.append result [|(head,1)|])
        | [] -> result
    _check aList [||]

> letterCheck "hello";;
val it : (char * int) array = [|('h', 1); ('e', 1); ('l', 2); ('o', 1)|]

Or have all the fun with:

"hello" |> Seq.countBy id

Related


F# undefined type error when annotating variable

Quantum I have a function with a recursive function that counts the letters in a word to "hello"return [|("h",1);("e",1);("l",2);("o",1);|]. My code is as follows: let letterCheck (a:string) = let aList = a |> Seq.toList; let rec _check (charList:char

F# undefined type error when annotating variable

Quantum I have a function with a recursive function that counts the letters in a word to "hello"return [|("h",1);("e",1);("l",2);("o",1);|]. My code is as follows: let letterCheck (a:string) = let aList = a |> Seq.toList; let rec _check (charList:char

F# undefined type error when annotating variable

Quantum I have a function with a recursive function that counts the letters in a word to "hello"return [|("h",1);("e",1);("l",2);("o",1);|]. My code is as follows: let letterCheck (a:string) = let aList = a |> Seq.toList; let rec _check (charList:char

F# undefined type error when annotating variable

Quantum I have a function with a recursive function that counts the letters in a word to "hello"return [|("h",1);("e",1);("l",2);("o",1);|]. My code is as follows: let letterCheck (a:string) = let aList = a |> Seq.toList; let rec _check (charList:char

F# undefined type error when annotating variable

Quantum I have a function with a recursive function that counts the letters in a word to "hello"return [|("h",1);("e",1);("l",2);("o",1);|]. My code is as follows: let letterCheck (a:string) = let aList = a |> Seq.toList; let rec _check (charList:char

F# undefined type error when annotating variable

Quantum I have a function with a recursive function that counts the letters in a word to "hello"return [|("h",1);("e",1);("l",2);("o",1);|]. My code is as follows: let letterCheck (a:string) = let aList = a |> Seq.toList; let rec _check (charList:char

F# undefined type error when annotating variable

Quantum I have a function with a recursive function that counts the letters in a word to "hello"return [|("h",1);("e",1);("l",2);("o",1);|]. My code is as follows: let letterCheck (a:string) = let aList = a |> Seq.toList; let rec _check (charList:char

F# undefined type error when annotating variable

Quantum I have a function with a recursive function that counts the letters in a word to "hello"return [|("h",1);("e",1);("l",2);("o",1);|]. My code is as follows: let letterCheck (a:string) = let aList = a |> Seq.toList; let rec _check (charList:char

F# undefined type error when annotating variable

Quantum I have a function with a recursive function that counts the letters in a word to "hello"return [|("h",1);("e",1);("l",2);("o",1);|]. My code is as follows: let letterCheck (a:string) = let aList = a |> Seq.toList; let rec _check (charList:char

F# undefined type error when annotating variable

Quantum I have a function with a recursive function that counts the letters in a word to "hello"return [|("h",1);("e",1);("l",2);("o",1);|]. My code is as follows: let letterCheck (a:string) = let aList = a |> Seq.toList; let rec _check (charList:char

Error when showing undefined variable

Shah Error: _co.bellowContent is undefinedThere was an error displaying the data . You can quote my code below. There was an error Error: _co.bellowContent is undefineddisplaying the data . You can quote my code below. app.component.html <table class="table">

Error when showing undefined variable

Shah Error: _co.bellowContent is undefinedThere was an error displaying the data . You can quote my code below. There was an error Error: _co.bellowContent is undefineddisplaying the data . You can quote my code below. app.component.html <table class="table">

Error when showing undefined variable

Shah Error: _co.bellowContent is undefinedThere was an error displaying the data . You can quote my code below. There was an error Error: _co.bellowContent is undefineddisplaying the data . You can quote my code below. app.component.html <table class="table">

MatPlotLib key error: 0 when annotating

prom In this code, I'm trying to create a bar chart with one bar (as the "total" bar, which will be placed next to the other bars) and part of a data frame. It works for another slice of the dataframe, but for the "total" slice (only one row), I keep getting t

Undefined variable error when defining variable

Julien Vincent When defining a variable, I get an "undefined variable" error. bellow is my code <?php $imagepath = $_SESSION['path']; require_once('class-db.php'); if ( !class_exists('INSERT') ) { class INSERT { public function post($postdata) {

Undefined variable error when defining variable

Julien Vincent When defining a variable, I get an "undefined variable" error. bellow is my code <?php $imagepath = $_SESSION['path']; require_once('class-db.php'); if ( !class_exists('INSERT') ) { class INSERT { public function post($postdata) {

How does an undefined variable throw a type error?

1252748 I have a user that is getting the error TypeError: a is undefined I'm confused how this happened. Won't trying to access an undefined variable raise a reference error? Under what circumstances would a type error be thrown? Disi As @jgillich pointed ou

How does an undefined variable throw a type error?

1252748 I have a user that is getting the error TypeError: a is undefined I'm confused how this happened. Won't trying to access an undefined variable raise a reference error? Under what circumstances would a type error be thrown? Disi As @jgillich pointed ou

How does an undefined variable throw a type error?

1252748 I have a user that is getting the error TypeError: a is undefined I'm confused how this happened. Won't trying to access an undefined variable raise a reference error? Under what circumstances would a type error be thrown? Disi As @jgillich pointed ou

extern undefined reference error when declaring variable

nepp95 I have 2 header files and 1 source file to work with. They are: Utility.h, Game.hand main.cpp. I have declared the external variable in . Utility.hand tried in main.cpp. This gave me an undefined reference error which I don't understand. I use the varia

"Undefined variable" error when running tcsh script

Senna I'm trying to execute every script that has "~SAC" at the end of the filename. For this, I wrote tc shell script using "foreach" like the code below. However, when I execute it by typing ./run.tcsh, it just shows "tmpdir: Undefined variable". ("tmpdir" i

Annotating variable list in PHP

Alec I have a list of objects which can be retrieved by: list($var1, $var2, $var3) = $helper->getStuff(); Except longer. Now, all these variables belong to the same class, Fooand I want to annotate them so that the IDE (PHPStorm) does what I'm doing. Usually,