Skip to content

Rules involving lists #2285

Answered by XiaowenHu96
MattHood asked this question in Q&A
Jun 15, 2022 · 1 comments · 1 reply
Discussion options

You must be logged in to vote
.type List = [ head: number, tail: List]

.decl Intervals(x : List)
Intervals([1,[3,[4,nil]]]).

.decl SumIntervals(before:List, after: List)
SumIntervals(xs, xs) :- Intervals(xs).
SumIntervals(xs, [z + y, zs]) :- 
    SumIntervals([x, xs], [y, ys]), xs = [z, zs].
.output SumIntervals(IO=stdout)

.decl Result(before: List, after: List)
Result(xs, ys) :- SumIntervals(xs, ys), ys = [y, nil].
Result([x, xs], [y, zs]) :- 
    SumIntervals([x, xs], [y, ys]), Result(ys, zs).
.output Result(IO=stdout)

After that, extract the longest list and add the value of Root to each of the elements.
I'm just unaware of any beginner-friendly textbook, all the textbooks I know for Datalog are all too theoreti…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@MattHood
Comment options

Answer selected by MattHood
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants