type alias Bag a = List (a, Int)
Run Code Online (Sandbox Code Playgroud)
我正在尝试创建与此类型别名一起使用的函数,但我不知道如何从函数返回 Bag 。就像在下面的函数中一样,我希望将一个新项目插入到现有的 bag 中(可能是空的) ,这是我目前正在尝试开始工作的唯一情况)。
insert : a -> Bag a -> Bag a
insert b c = case c of
[] -> Bag a : [(b, 1)]
--this case below is what I'm hoping could work out..cant really check without getting the first case to work---
x::xs -> if Tuple.first x == b
then Bag a : [(b, (Tuple.second x) + 1)] ++ xs
else [x] ++ insert b xs …Run Code Online (Sandbox Code Playgroud)