模式匹配中的F#小于算子
由于某些原因,此模式匹配中的小于运算符将不起作用。这是我唯一的错误,让我发疯。 我可能错过了一些非常明显的东西,但非常感谢所有的帮助模式匹配中的F#小于算子,f#,pattern-matching,F#,Pattern Matching,由于某些原因,此模式匹配中的小于运算符将不起作用。这是我唯一的错误,让我发疯。 我可能错过了一些非常明显的东西,但非常感谢所有的帮助 let CheckAccount account = match account with | {Balance < 10.00} -> Console.WriteLine("Balance is Low") | {Balance >= 10.00 and <= 100.00} -> Console.WriteLine("Balan
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")
让支票帐户=
对帐
|{Balance<10.00}->Console.WriteLine(“余额低”)
|{Balance>=10.00和Console.WriteLine(“平衡正常”)
|{Balance>100.00}->Console.WriteLine(“余额高”)
这是一种:
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())
type Account={AccountNumber:string
可变平衡:浮动}
此成员。撤消(amnt:float)=
如果amnt>this.Balance,则
Console.WriteLine(“无法提取。您希望提取的金额大于您当前的余额。”)
其他的
this.Balance您可以使用模式匹配提取余额值,将其绑定到新名称,然后使用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")
然后您可以使用这些选项:
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和10以上< /> >
非常感谢!在写这个答案的过程中我非常分心:-用活动模式添加了更多的东西,而这里提供的答案解决了P问题,我仍然想知道为什么<不起作用。原因是什么?在我看来,任何见解都是对这个答案的一个非常有价值的补充。谢谢!
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")