How to add elements from sublists with 2 elements

2019-07-29 22:35发布

I'm working on a list that contains sublists with 2 elements each. The first element of each sublist is a string and the second one a number.

[ [e, 30], [a, 170], [k, 15], [e, 50] ] 

I want to add all the numbers of each sublist. I tried this one:

sum_fire([H|T],S):-
  flatten(H,L),
  sum_fire(T,S),
  L=[_H1|T1],
  sum(T1,S).

but it's completely wrong, I think. How can i get this to work?

标签: prolog
3条回答
等我变得足够好
2楼-- · 2019-07-29 22:54

SWI-Prolog has library(aggregate) for that:

sum_fire(L, S) :-
  aggregate_all(sum(X), member([_,X], L), S).

Another way to get the task done, using library(apply) and library(lists):

?- maplist(nth1(2), [ [e, 30], [a, 170], [k, 15], [e, 50] ], L), sum_list(L, S).
L = [30, 170, 15, 50],
S = 265.
查看更多
Melony?
3楼-- · 2019-07-29 23:00

Nothing wrong with @mbratch's code (+1), but I would do it tail-recursively (and cut-free) like so:

sum_fire(L, Sum) :- sum_fire(L, 0, Sum).

sum_fire([[_,N]|T], Acc, Sum) :-
    Acc1 is N + Acc,
    sum_fire(T, Acc1, Sum).
sum_fire([], Sum, Sum).
查看更多
forever°为你锁心
4楼-- · 2019-07-29 23:05

You just need to break out the string versus the number:

sum_fire( [[_,N]|Tail], Sum ) :-
    sum_fire( Tail, S1 ),
    Sum is N + S1.
sum_fire( [], 0 ).

So I'm using [_,N] instead of H for the head item because I want what's inside (the number N). I don't care about the string for the sum, so it's _.

查看更多
登录 后发表回答