Detailed explanations/diagrams can be found here

http://www.delphiforfun.or

http://www.cs.auc.dk/~norm

http://www.tvcc.cc/staff/f

Solved

Posted on 2005-05-07

generate(N, N, [N]).

generate(N, M, [N | Rest]) :-

N < M,

N1 is N + 1,

generate(N1, M, Rest).

solution(N, Ys):-

generate(1, N, RFvals),

Bot is 1 - N, Top is N - 1,

generate(Bot, Top, Uvals),

TwiceN is N + N,

generate(2, TwiceN, Vvals),

solution(Ys,RFvals, RFvals, Uvals, Vvals).

solution([], [], _, _, _).

solution([Y | Ylist], [X | Xsleft], Ys, Us, Vs) :-

insert(Y, Ysleft, Ys),

U is X - Y,

insert(U, Usleft, Us),

V is X + Y,

insert(V, Vsleft, Vs),

solution(Ylist, Xsleft, Ysleft, Usleft, Vsleft).

insert(X, L, [X | L]).

insert(X, [H | T], [H | XT]) :-

insert(X, T, XT).

Hi experts, I would like you to help me understanding the solution above written in PROLOG. Please provide me with a detailed explaination as soon as possible.

generate(N, M, [N | Rest]) :-

N < M,

N1 is N + 1,

generate(N1, M, Rest).

solution(N, Ys):-

generate(1, N, RFvals),

Bot is 1 - N, Top is N - 1,

generate(Bot, Top, Uvals),

TwiceN is N + N,

generate(2, TwiceN, Vvals),

solution(Ys,RFvals, RFvals, Uvals, Vvals).

solution([], [], _, _, _).

solution([Y | Ylist], [X | Xsleft], Ys, Us, Vs) :-

insert(Y, Ysleft, Ys),

U is X - Y,

insert(U, Usleft, Us),

V is X + Y,

insert(V, Vsleft, Vs),

solution(Ylist, Xsleft, Ysleft, Usleft, Vsleft).

insert(X, L, [X | L]).

insert(X, [H | T], [H | XT]) :-

insert(X, T, XT).

Hi experts, I would like you to help me understanding the solution above written in PROLOG. Please provide me with a detailed explaination as soon as possible.

2 Comments

Detailed explanations/diagrams can be found here

http://www.delphiforfun.or

http://www.cs.auc.dk/~norm

http://www.tvcc.cc/staff/f

Title | # Comments | Views | Activity |
---|---|---|---|

bigDiff challenge | 17 | 60 | |

isEverywhere challenge | 19 | 49 | |

zeroMAx challenge | 20 | 66 | |

scoresSpecial challenge | 13 | 35 |

This article will show, step by step, how to integrate R code into a R Sweave document

Join the community of 500,000 technology professionals and ask your questions.

Connect with top rated Experts

**24** Experts available now in Live!