F# 小于模式匹配中的运算符
F# less than operator in pattern matching
由于某种原因,此模式匹配中的小于运算符将不起作用。这是我唯一的错误,它让我发疯。
我可能遗漏了一些非常明显的东西,但感谢所有帮助。
let CheckAccount account =
match account with
| {Balance < 10.00} -> Console.WriteLine("Balance is Low")
| {Balance >= 10.00 and <= 100.00} -> Console.WriteLine("Balance is OK")
| {Balance > 100.00} -> Console.WriteLine("Balance is High")
这是类型:
type Account = {AccountNumber:string
mutable Balance:float}
member this.Withdraw(amnt:float) =
if amnt > this.Balance then
Console.WriteLine("Unable to withdraw. The Amount you wish to withdraw is greater than your current balance.")
else
this.Balance <- this.Balance - amnt
Console.WriteLine("You have withdrawn £" + amnt.ToString() + ". Your balance is now: £" + this.Balance.ToString())
member this.Deposit(amnt:float) =
this.Balance <- this.Balance + amnt
Console.WriteLine("£" + amnt.ToString() + " Deposited. Your new Balance is: £" + this.Balance.ToString())
member this.Print =
Console.WriteLine("Account Number: " + this.AccountNumber)
Console.WriteLine("Balance: £" + this.Balance.ToString())
您可以使用模式匹配来提取余额值,将其绑定到一个新名称,然后使用 when
子句比较这些值:
let CheckAccount account =
match account with
| {Balance = b} when b < 10.00 -> Console.WriteLine("Balance is Low")
| {Balance = b} when b >= 10.00 && b <= 100.00 -> Console.WriteLine("Balance is OK")
| {Balance = b} when b > 100.00 -> Console.WriteLine("Balance is High")
我要说的是,在这种情况下,您实际上并没有从使用模式匹配中得到多少。如果您使用 if
编写相同的代码,那么它可能看起来会更好。
您可以使用更高级的方法并定义可让您比较值的活动模式:
let (|LessThan|_|) k value = if value < k then Some() else None
let (|MoreThan|_|) k value = if value > k then Some() else None
那么你可以改用那些:
let CheckAccount account =
match account with
| {Balance = LessThan 10.0} -> Console.WriteLine("Balance is Low")
| {Balance = LessThan 100.0 & MoreThan 10.0 } -> Console.WriteLine("Balance is OK")
这实际上相当有趣 - 因为您可以使用 &
构造来组合多个活动模式,如 LessThan 100.0 & MoreThan 10.0
.
由于某种原因,此模式匹配中的小于运算符将不起作用。这是我唯一的错误,它让我发疯。 我可能遗漏了一些非常明显的东西,但感谢所有帮助。
let CheckAccount account =
match account with
| {Balance < 10.00} -> Console.WriteLine("Balance is Low")
| {Balance >= 10.00 and <= 100.00} -> Console.WriteLine("Balance is OK")
| {Balance > 100.00} -> Console.WriteLine("Balance is High")
这是类型:
type Account = {AccountNumber:string
mutable Balance:float}
member this.Withdraw(amnt:float) =
if amnt > this.Balance then
Console.WriteLine("Unable to withdraw. The Amount you wish to withdraw is greater than your current balance.")
else
this.Balance <- this.Balance - amnt
Console.WriteLine("You have withdrawn £" + amnt.ToString() + ". Your balance is now: £" + this.Balance.ToString())
member this.Deposit(amnt:float) =
this.Balance <- this.Balance + amnt
Console.WriteLine("£" + amnt.ToString() + " Deposited. Your new Balance is: £" + this.Balance.ToString())
member this.Print =
Console.WriteLine("Account Number: " + this.AccountNumber)
Console.WriteLine("Balance: £" + this.Balance.ToString())
您可以使用模式匹配来提取余额值,将其绑定到一个新名称,然后使用 when
子句比较这些值:
let CheckAccount account =
match account with
| {Balance = b} when b < 10.00 -> Console.WriteLine("Balance is Low")
| {Balance = b} when b >= 10.00 && b <= 100.00 -> Console.WriteLine("Balance is OK")
| {Balance = b} when b > 100.00 -> Console.WriteLine("Balance is High")
我要说的是,在这种情况下,您实际上并没有从使用模式匹配中得到多少。如果您使用 if
编写相同的代码,那么它可能看起来会更好。
您可以使用更高级的方法并定义可让您比较值的活动模式:
let (|LessThan|_|) k value = if value < k then Some() else None
let (|MoreThan|_|) k value = if value > k then Some() else None
那么你可以改用那些:
let CheckAccount account =
match account with
| {Balance = LessThan 10.0} -> Console.WriteLine("Balance is Low")
| {Balance = LessThan 100.0 & MoreThan 10.0 } -> Console.WriteLine("Balance is OK")
这实际上相当有趣 - 因为您可以使用 &
构造来组合多个活动模式,如 LessThan 100.0 & MoreThan 10.0
.