F #: warning FS0020: this expression must be of type 'unit', but of type 'bool'

I am trying to learn F # by looking at some Euler problems, and I found a problem that I could not understand. This is my naive decision.

let compute =
    let mutable f = false
    let mutable nr = 0
    while f = false do
        nr <- nr + 20
        f = checkMod nr
    nr

When I do this, I get an error with the error message FS0020: This expression must be of type "unit", but it must be of type "bool" in the expression "nr <- nr +20". I tried rewriting and moving expressions, and I always get this error in the line under the while statement.

I am writing this using the beta version of VS2010.

+3
source share
6 answers

Next line:

f = checkMod nr

- , , , , . :

f <- checkMod nr

. , , ...

, while f = false do while not f do; .

, , F # . ( F #), ( ) , . . , - .

+10

, WEG- "" FS0020, , , .

, :

// you are calling a function for its side-effects, intend to ignore result    
let Example1Orig() =
    let sb = new System.Text.StringBuilder()
    sb.Append("hi")       // warning FS0020
    sb.Append(" there")   // warning FS0020
    sb.ToString()

let Example1Fixed() =
    let sb = new System.Text.StringBuilder()
    sb.Append("hi") |> ignore
    sb.Append(" there") |> ignore
    sb.ToString()

, ( ):

// the warning is telling you useful info 
// (e.g. function does not have an effect, rather returns a value)
let Example2Orig() =
    let l = [1;2;3] 
    List.map (fun x -> x * 2) l    // warning FS0020
    printfn "doubled list is %A" l

let Example2Fixed() =
    let l = [1;2;3] 
    let result = List.map (fun x -> x * 2) l
    printfn "doubled list is %A" result

:

// '=' versus '<-'
let Example3Orig() =
    let mutable x = 3
    x = x + 1          // warning FS0020
    printfn "%d" x    

let Example3Fixed() =
    let mutable x = 3
    x <- x + 1
    printfn "%d" x    
+12

, .

, :

let nr =
   let rec compute nr =  
      if checkMod nr then nr else compute (nr + 20)
   compute 0     
+1

while F # , . while unit ( void ++/#). unit.

:

nr <- nr + 20

unit,

f = checkMod nr

bool Noldorin. . , . :

#nowarn "0020"
0

, .

20, checkMod. . , , , . , ( 20), , :

let multi20 = Seq.initInfinite (fun i -> i*20)
let compute = multi20 |> Seq.find checkMod

let ( ). , . - , , , , , .

,

let computeCryptic = Seq.initInfinite ((*) 20) |> Seq.find checkMod

, , , .

0

, , FS0020: .

F #, , ( gdp...) FS0020: 'unit', '(string → ^ a → unit) * string * float '. , ; printfn, . .

for country in wb.Regions.``Arab World``.Countries do
  let gdp = country.Indicators.``GDP per capita (current US$)``.[2010]
  let gdpThous = gdp / 1.0e3
  printfn "%s, %s (%.2f)" country.Name, country.CapitalCity, gdpThous
0

Source: https://habr.com/ru/post/1712285/


All Articles