5
votes

The question: is there a place with some programs that I can check out? I'm talking rosetta code style, but I went there and saw that almost every program is solved with non pure prolog syntax (using cut, using IS, that type of thing), which doesn't help me because of my studies requirements.

So is there any half good source? I'm talking small programs like inserting an element at a set place and so on, I find I learn way more when I can check whether or not I am doing things right

Hi everyone, I am learning prolog and I've found prolog Now! which has helped me to learn more prolog in two quick reads than in 2 months of class. I even solved a few exercises correctly which was a surprise to me. So I want to keep this track (I've done many of the exercises up to chapter five, and I found a couple githubs where I can check if I am close to a solution, if my solution misses cases, and so on) Thanks in advance

For what is worth I'm talking something like this, if it could with more than 3 examples better but this type of problems

http://www.irisa.fr/prive/ridoux/ICLP91/node7.html#SECTION00031000000000000000

1
Why is it so important that there is no cut, etc.?Willem Van Onsem
I've been told to use pure prolog syntax for this program and later on I assume I will use it. Also because even though I can't solve the problem right now, it is likely that type of operators aren't needed at all @WillemVanOnsemkeont
SO is a good place for this. So no need to ask. Just look at answers.false
I have started doing that today @false , main problem as a begginer is not knowing what to ask or what to look for. But yeah, less is nothingkeont
Official Learn Prolog Now! website and using your favorite search engine just search for lpn exercise solutions for a list of various github repositories. Also interestingG_V

1 Answers

2
votes

Paul Tarau provided a test bed for hitchhiker Prolog, with several interesting pure Prolog 'programs': queens, sudoku, lambdas, peano arithmetic. See the folder progs or run my implementation (from hhprolog) as snippets right here...

N-Queens, a very elegant solution I had never seen before:

place_queen(I,[I|_],[I|_],[I|_]).
place_queen(I,[_|Cs],[_|Us],[_|Ds]):-place_queen(I,Cs,Us,Ds).

place_queens([],_,_,_).
place_queens([I|Is],Cs,Us,[_|Ds]):-
  place_queens(Is,Cs,[_|Us],Ds),
  place_queen(I,Cs,Us,Ds).

gen_places([],[]).
gen_places([_|Qs],[_|Ps]):-gen_places(Qs,Ps).

qs(Qs,Ps):-gen_places(Qs,Ps),place_queens(Qs,Ps,_,_).

goal(Ps):-qs([0,1,2,3,4,5],Ps).

function runnable(out_elem) {
    settext(out_elem = out_elem || run_output)
    return new Prog({
        writeln: function(l) {
            textln(out_elem, l)
        },
        nl_source: `
            place_queen I _0 _1 _2 and
              _0 holds list I _3 and
              _1 holds list I _4 and
              _2 holds list I _5 .

            place_queen I _0 _1 _2 and
              _0 holds list _3 Cs and
              _1 holds list _4 Us and
              _2 holds list _5 Ds 
            if
              place_queen I Cs Us Ds .

            place_queens nil _0 _1 _2 .

            place_queens _0 Cs Us _1 and
              _0 holds list I Is and
              _1 holds list _2 Ds 
            if
              place_queens Is Cs _3 Ds and
              _3 holds list _4 Us and
              place_queen I Cs Us Ds .

            gen_places nil nil .

            gen_places _0 _1 and
              _0 holds list _2 Qs and
              _1 holds list _3 Ps 
            if
              gen_places Qs Ps .

            qs Qs Ps 
            if
              gen_places Qs Ps and
              place_queens Qs Ps _0 _1 .

            goal Ps 
            if
              qs _0 Ps and
              _0 lists 0 1 2 3 4 5 .
        `
    })
}
function settext(e, t) {
    e.innerHTML = t || ''
}
function textln(e, l) {
    e.innerHTML = e.innerHTML + `\n` + (l || '')
}

const prog = runnable()
const t0 = Date.now()
prog.run(print_sol.checked)
prog.options.writeln('elapsed secs:' + (Date.now() - t0) / 1000)
        .boxed {
            box-shadow: 8px 8px 5px #444;
            background-image: linear-gradient(180deg, #fff, #ddd 40%, #ccc);
        }
        .spacing {
            border: 1px solid #333;
            padding: 8px 12px;
        }
<script src="https://cdn.jsdelivr.net/gh/CapelliC/hhprolog/hhprolog.js"></script>
<label>print solutions
    <input id=print_sol type=checkbox checked=checked>
</label>
<div class="boxed spacing">
    <div>Solutions</div>
    <pre id=run_output></pre>
</div>