F#交换数据数组并获得正确的结果

Joh*_*ohn 3 f#

let highs = [| 2; 4; 6 |]
let lows = [| 1; 5; 10 |]
Run Code Online (Sandbox Code Playgroud)

我想从上面得到2个数组:如果highs中的元素小于lows中的相应元素,则交换它们.所以,我可以得到最后的2个数组:

let trueHighs = [| 2; 5; 10 |]
let trueLows = [| 1; 4; 6 |]
Run Code Online (Sandbox Code Playgroud)

我该怎么做呢?

pad*_*pad 5

与JaredPar的答案类似,但更简单:

let trueHighs, trueLows =        
    Array.zip highs lows
    |> Array.map (fun (x, y) -> if x >= y then (x, y) else (y, x))
    |> Array.unzip
Run Code Online (Sandbox Code Playgroud)

另一个更简洁的版本:

let trueHighs, trueLows =        
    (highs, lows)
    ||> Array.map2 (fun x y -> if x >= y then (x, y) else (y, x))    
    |> Array.unzip
Run Code Online (Sandbox Code Playgroud)