总结两个年龄段之间人员的总余额 [英] Sum the total balance for people between two ages
问题描述
我试图计算在银行内拥有账户的两个年龄段之间的人/客户的总余额。
这些是构造函数
类型NI = Int
类型Age = Int
类型余额= Int
类型Person =(NI,年龄,余额)
类型Bank = [人]
这是银行
rbs :: Bank
rbs = [(1,73,1000)
,(2,18,-50)
,(3,60 ,190)
,(4,26,300)
,(5,24,456)
,(6,32,7500)
,(7,41, - (8,59,-850)
,(9,44,348)
,(10,66,-1000)
,(11,37, 20000)
,(12,29,-245)
,(13,55,3090)
]
这是我的代码递归检查银行
equityA':: Bank - > (Int,Int) - >银行
(s, pre>
权益A'((n,a,b):xs)(0,0)=错误未选择年龄
equityA'[](x,y)= [] $ b $ (a,b):xs)(f,s)= if(f <= a)&& (f,s)
如果我运行equityA'rbs(40,50),输出将是[(7,41,-46),(9,44,348)]
我正在努力做的是打印出这些人的总余额。
代码检查总数。
equityAge :: Bank - > (Int,Int) - > Int
equityAge((n,a,b):xs)(0,0)=错误没有选择年龄
equityAge [](x,y)= 0
equityAge (n,a,b):xs)(f,s)=
任何帮助。
解决方案如果您使用 list comprehensions :
equityAge :: Bank - > (Int,Int) - > Int
equityAge _(0,0)=错误没有选择年龄
equityAge [](x,y)= 0
equityAge x(f,s)= sum [b | (n,a,b)< -x,f< = a,a< = s]
除此之外,你可以申请 Sarah's建议,并在您的原始功能上使用
map
和sum
:equityAge :: Bank - > (Int,Int) - > Int
equityAge _(0,0)=错误没有选择年龄
equityAge [](x,y)= 0
equityAge x(f,s)= sum(map( \(_,_,b) - > b)(equityA'x(f,s)))
I'm trying to calculate the total balance of people/clients between two ages that have an account within a bank. I can get it to display the list of people that fall under the requirement.
These are the constructors
type NI = Int type Age = Int type Balance = Int type Person = (NI, Age, Balance) type Bank = [Person]
This is the Bank
rbs :: Bank rbs = [ (1, 73, 1000) , (2, 18, -50) , (3, 60, 190) , (4, 26, 300) , (5, 24, 456) , (6, 32, 7500) , (7, 41, -46) , (8, 59, -850) , (9, 44, 348) , (10, 66, -1000) , (11, 37, 20000) , (12, 29, -245) , (13, 55, 3090) ]
And this is my code to recursively check the bank
equityA' :: Bank -> (Int, Int) -> Bank equityA' ((n,a,b):xs) (0,0) = error "No ages were selected" equityA' [] (x,y) = [] equityA' ((n,a,b):xs) (f, s) = if (f <= a) && (s >= a) then (n,a,b) : equityA' xs (f, s) else equityA' xs (f, s)
If I run equityA' rbs (40,50) the output will be [(7,41,-46),(9,44,348)]
What i'm struggling to do is to is print out the total balance of those people. I have some code down but am stuck at the actual calculation part.
The code for to check the total.
equityAge :: Bank -> (Int, Int) -> Int equityAge ((n,a,b):xs) (0,0) = error "No ages were selected" equityAge [] (x,y) = 0 equityAge ((n,a,b):xs) (f, s) =
I would be grateful for any help.
解决方案It's quite simple if you use list comprehensions:
equityAge :: Bank -> (Int,Int) -> Int equityAge _ (0,0) = error "No ages were selected" equityAge [] (x,y) = 0 equityAge x (f,s) = sum [ b | (n,a,b) <-x, f <= a, a <= s]
Other than that you can apply Sarah's advice and use
map
andsum
on your original function:equityAge :: Bank -> (Int,Int) -> Int equityAge _ (0,0) = error "No ages were selected" equityAge [] (x,y) = 0 equityAge x (f,s) = sum (map(\(_,_,b) -> b) (equityA' x (f,s)))
这篇关于总结两个年龄段之间人员的总余额的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!