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.
Next line:
f = checkMod nr
- , , , , . :
f <- checkMod nr
. , , ...
, while f = false do while not f do; .
while f = false do
while not f do
, , F # . ( F #), ( ) , . . , - .
, 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
, .
let nr = let rec compute nr = if checkMod nr then nr else compute (nr + 20) compute 0
while F # , . while unit ( void ++/#). unit.
while
unit
void
nr <- nr + 20
unit,
bool Noldorin. . , . :
bool
#nowarn "0020"
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
, , , .
, , 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
Source: https://habr.com/ru/post/1712285/More articles:What is swingset2 - javaМожно ли переопределить метод UILabel drawTextInRect, чтобы изменить размер текста UILabel? - objective-cSolve boost.thread compilation error with Metrowerks compiler - c ++Android emulator output - androidjava ejb3 @PostConstruct - javaWant a REST api request to include a good idea in a cookie? - restJava: parsing ms-word document using POI / HWPF - javaCreating keyboard keys - cRegexp to match Javascript string literals with a specific keyword using Java - javaIPhone development start - iosAll Articles