Program in Prolog for sum of numbers in interval

前端 未结 4 1240
粉色の甜心
粉色の甜心 2021-01-21 19:30

I\'m trying to create a program in Prolog which takes two numbers - A and B and finds the sum of the numbers from A to B, including them.

To sum up: sum(1, 5, C) should

4条回答
  •  没有蜡笔的小新
    2021-01-21 19:54

    Available in SWI-Prolog, library(aggregate) is a full fledged interface:

    ?- aggregate(sum(N), between(1,5,N), S).
    S = 15.
    

    it does internally a similar work as you can read in Kaarel' answer. Anyway, to learn Prolog, follow C.B. detailed explanation.

提交回复
热议问题