8-queens solution in PROLOG explaination needed (I am a beginer!!)

Posted on 2005-05-07
Last Modified: 2012-05-05
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.  
Question by:Al-Qershi
    LVL 45

    Accepted Solution

    This is the recursive solution to 8 queens problem. In one line - take the list of board layouts from solutions to the "n-1 Queens Problem" and for each board, try adding a queen to each of the rows in the n-th column. If the new queen is "safe", add this board to the set of solutions to the "n Queens Problem".

    Detailed explanations/diagrams can be found here
    LVL 45

    Expert Comment

    Why the C grade?

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    bigDiff challenge 17 60
    isEverywhere  challenge 19 49
    zeroMAx challenge 20 66
    scoresSpecial  challenge 13 35
    If you haven’t already, I encourage you to read the first article ( in my series to gain a basic foundation of R and R Studio.  You will also find the …
    This article will show, step by step, how to integrate R code into a R Sweave document
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
    This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

    754 members asked questions and received personalized solutions in the past 7 days.

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

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now