Prolog- How to sum only part of list inside another list?

前端 未结 2 750
盖世英雄少女心
盖世英雄少女心 2021-01-24 06:01

Define a recursive rule, in the form of sum(Lst, Total), which can calculate the sum of a list of stated, where Lst is in the format of [[s1, p1], [s2, p2],

相关标签:
2条回答
  • 2021-01-24 06:40

    You're only missing the base case where the list is empty!

    sum([],0).
    
    sum([[_,X]|T],Total) :- 
        sum(T,Rest),
        Total is X + Rest. 
    
    
    0 讨论(0)
  • 2021-01-24 06:49

    sum(L,Total):- total(L,0,Total).

    sum([],Total,Total).

    sum([[_|HT]T],Temp,Total) :- Temp1 is temp+HT,total(T,Temp1,Total).

    0 讨论(0)
提交回复
热议问题