4
了一個錯誤:F#這是鑑於以下代碼Option.map
let mapOption (f : ('a -> 'b)) (x : 'a option) =
match x with
| Some x -> Some(f(x))
| None -> None
let mapOptions (f : ('a -> 'b)) (xs : 'a option list) : 'b option list =
xs
|> List.map (fun (x : 'a option) -> mapOption f x)
let myList = [None; Some 1; Some 2; None]
let a = myList |> mapOptions (fun x -> x + 2)
let b = myList |> List.map(fun x-> x |> Option.map(fun y -> y + 2))
爲什麼A和B等於結果:
[null; Some 3; Some 4; null]
或val it : int option list = [null; Some 3; Some 4; null]
豈不是:
[None; Some 3; Some 4; None]
該行爲(即'None = null')在規範中明確記錄。一些更多的討論在這裏:http://stackoverflow.com/questions/10435052/f-passing-none-to-function-getting-null-as-parameter-value –