我在CPN ML工作,这与SML大致相同。 我确信以下部分代码可以正常工作:
color Product = string;
color Number = int;
color StockItem = record prod:Product * number:Number;
color Stock = list StockItem;
var x:StockItem; var s:Stock;
fun incrs(x:StockItem,s:Stock) =
if s=[] then [x]
else (if (#prod(hd(s)))=(#prod(x))
then {prod=(#prod(hd(s))),number=((#number(hd(s)))+(#number(x)))}::tl(s)
else hd(s):: incrs(x,tl(s)));
fun decrs(x:StockItem,s:Stock)= incrs({prod=(#prod(x)),
number=(~(#number(x)))},s)
现在我的问题是我需要为每个商店(学生任务)保留商店。所以我做了以下改编:
color Store = string;
color StockItem = record prod:Product * number:Number * store:Store;
fun incrs(x:StockItem,s:Stock) =
if s=[]
then [x]
else (if {(#prod(hd(s)))=(#prod(x)), (#STORE(hd(s)))=(#STORE(x))}
then {prod=(#prod(hd(s))),
STORE = (#STORE(hd(s))),
number=((#number(hd(s)))+(#number(x)))}::tl(s)
else hd(s):: incrs(x,tl(s)));
最后一行代码似乎不起作用。有人可以帮我吗?非常感谢!!