鉴于以下组代表一个存储类型:F#对于列表中的每个元素,应用功能,在列表元素中,b
type Article = string
type Amount = int
type Price = int
type Desc = Amount*Price
type Stock = (Article*Desc) list
type Order = Article * Amount
type Status<'a> = Result of 'a | Error of string
我想打一个函数,它接受一个Order
和Stock
和回报一个Status<Price * Stock>
。下面get (a,k) st
所定义的函数的值是Result (p,st')
其中p
是k
件a
和st'
价格是通过除去k
件a
从st
获得的新的库存。
例子:
let rec get (a,k) st =
match st with
| (a',(n',p'))::rest when a'=a -> Result (p'*k,(a',(n'-k,p'))::rest)
| (a',(n',p'))::rest ->
match get (a,k) rest with
| Result (price,stock) -> Result (price,(a',(n',p'))::stock)
| Error e -> Error e
get ("a2", 10) st
val it : int * (string * (int * int)) list =
Result (200, [("a1", (100, 10)); ("a2", (40, 20)); ("a3", (25, 40))])
现在,我怎么会去这样做,如果我花了一个Order list
?
如get [("a2",10);("a1",10)] st
将返回Result (300, [("a1", (90, 10)); ("a2", (40, 20)); ("a3", (25, 40))])
你'get''代码失败'无与伦比 '['' – TheQuickBrownFox
固定编译 - 笨手笨脚。 – Khaine775
我不明白您的预期产出中的数字是如何计算的。减法应该发生在'a2'上吗?为什么'300'? –