get_sublist in prolog

The hardest thing when programming prolog, is to change your mind. In fact declarative programming (while powerful) is hard to get in touch. I always tend to think imperatively, even if I attended formal logic lessons in the University.

Now just a couple of easy things… I’m a beginner.

get_sublist(Start, Len, List, Sub) :- append(L1, L2, List),
length(L1, Start), append(Sub, _, L2), length(Sub, Len).

This way you get a sublist. The style is declarative. The efficience is …. well we ll’see later.
An “imperative” version is

get_sublist(_, 0, _, []).
get_sublist(Start, Len, List, [H | Acc]) :- nth0(Start, List, H),
succ(Start, NStart), succ(NLen, Len),
get_sublist(NStart, NLen, List, Acc).

It’s not as clean, but should be more efficient. It uses an accumulator variable. In fact this algorithm is not really declarative. We could say that it does the same thing than

def get_sublist(start, len, list, acc)
if len > 0
acc.push list[start]
nstart = start + 1
nlen = len - 1
get_sublist(nstart, nlen, list, acc)
end
end

Incidentally this is ruby code, you can test it with

l = []
get_sublist(2, 3, %w{ a b c d e f g}, l)
l.each { |e| puts e}

And now… lets see if we optimized or not. This is comes from SWIProlog.

5 ?- time(get_sublist(2, 3, [a, b, c, d, e, f], Acc)).
% 22 inferences, 0.00 CPU in 0.00 seconds (0% CPU, Infinite Lips)

Acc = [c, d, e]

Yes
6 ?- time(get_sublist_old(2, 3, [a, b, c, d, e, f], Acc)).
% 37 inferences, 0.00 CPU in 0.00 seconds (0% CPU, Infinite Lips)

Acc = [c, d, e]

Let’s try with bigger numbers…

24 ?- make_list(100000, L), time(get_sublist(2000, 500, L, Acc)).
% 378,001 inferences, 0.33 CPU in 0.42 seconds (78% CPU, 1145458 Lips)

L = [100000, 99999, 99998, 99997, 99996, 99995, 99994, 99993, 99992|...]
Acc = [98000, 97999, 97998, 97997, 97996, 97995, 97994, 97993, 97992|...]

Yes
25 ?- make_list(100000, L), time(get_sublist_old(2000, 500, L, Acc)).
% 15,007 inferences, 0.08 CPU in 0.09 seconds (87% CPU, 187587 Lips)

L = [100000, 99999, 99998, 99997, 99996, 99995, 99994, 99993, 99992|...]
Acc = [98000, 97999, 97998, 97997, 97996, 97995, 97994, 97993, 97992|...]

Yes
26 ?- make_list(100000, L), time(get_sublist_old(2000, 5000, L, Acc)).
% 42,007 inferences, 0.56 CPU in 0.66 seconds (85% CPU, 75013 Lips)

L = [100000, 99999, 99998, 99997, 99996, 99995, 99994, 99993, 99992|...]
Acc = [98000, 97999, 97998, 97997, 97996, 97995, 97994, 97993, 97992|...]

Yes
27 ?- make_list(100000, L), time(get_sublist(2000, 5000, L, Acc)).
% 7,530,001 inferences, 6.88 CPU in 8.82 seconds (78% CPU, 1094477 Lips)

L = [100000, 99999, 99998, 99997, 99996, 99995, 99994, 99993, 99992|...]
Acc = [98000, 97999, 97998, 97997, 97996, 97995, 97994, 97993, 97992|...]

Yes

It looks like we didn’t optimize very much. 😦
I should study more.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: