gpt4 book ai didi

f# - 为什么使用向后管道运算符可以解决编译错误?

转载 作者:行者123 更新时间:2023-12-01 07:35:35 24 4
gpt4 key购买 nike

编译器接受以下行:

input |> Prop.forAll <| fun (a , b) -> add a b = add b a

但是,当我用括号替换向后管道运算符时,我收到一个错误:
input |> Prop.forAll ( fun (a , b) -> add a b = add b a )

Type mismatch. Expecting a Arbitrary -> 'a but given a ('b -> 'c) -> Property The type 'Arbitrary' does not match the type ''a -> 'b



我不太确定这个错误是什么意思。为什么向后管道运算符编译但括号不编译?

附录:
module Arithmetic

let add a b =
a + b

open FsCheck
open FsCheck.Xunit

[<Property(MaxTest=1000, QuietOnSuccess=true)>]
let ``'a + 'b equals 'b + 'a`` () =

// Declare generators per type required for function
let intGenerator = Arb.generate<int>

// Map previously declared generators to a composite generator
// to reflect all parameter types for function
let compositeGenerator = (intGenerator , intGenerator) ||> Gen.map2(fun a b -> a , b)

// Pull values from our composite generator
let input = Arb.fromGen compositeGenerator

// Apply values as input to function
input |> Prop.forAll <| fun (a , b) -> add a b = add b a

最佳答案

在第二行,您的参数顺序错误。

函数应用程序具有最高优先级,因此它首先被应用,在其他任何事情之前。运营商<||>在此之后应用,并且它们具有相同的优先级,因此首先应用左边的,然后应用右边的。因此,如果您考虑这一行:

x |> y <| z

首先,您应用左管道并获得:
(y x) <| z

应用正确的管道后,您会得到:
y x z

但如果你考虑第二行,情况正好相反:
x <| y (z)

应用管道后:
y (z) x

关于f# - 为什么使用向后管道运算符可以解决编译错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38999133/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com