• No results found

Session 6: Accumulating parameters + graphs

N/A
N/A
Protected

Academic year: 2021

Share "Session 6: Accumulating parameters + graphs"

Copied!
17
0
0

Loading.... (view fulltext now)

Full text

(1)

Session 6:

Accumulating parameters +

graphs

(2)

sumlist([], 0).

sumlist([H|T], Sum)

:-sumlist(T, TmpSum), Sum is TmpSum + H.

?- sumlist([5,4,9,8], S).

(3)

sumlist([], 0). sumlist([H|T], Sum) :-sumlist(T, TmpSum), Sum is TmpSum + H. ?- sumlist([5,4,9,8], S). ?- sumlist([4,9,8],TmpS1), S is TmpS1 + 5. ?- sumlist([9,8],TmpS2), TmpS1 is TmpS2 + 4, S is TmpS1 + 5. ?- sumlist([8],TmpS3), TmpS2 is TmpS3 + 9, TmpS1 is TmpS2 + 4, S is TmpS1 + 5. ?- sumlist([],TmpS4), TmpS3 is TmpS4 + 8, TmpS2 is TmpS3 + 9, TmpS1 is TmpS2 + 4, S is TmpS1 + 5. ?- TmpS3 is 0 + 8, TmpS2 is TmpS3 + 9, TmpS1 is TmpS2 + 4, S is TmpS1 + 5. ?- TmpS2 is 8 + 9, TmpS1 is TmpS2 + 4, S is 5 + TmpS1. ?- TmpS1 is 17 + 4, S is 5 + TmpS1. ?- S is 5 + 21. 1 goal 2 goals 3 goals 4 goals 5 goals 4 goals 3 goals 2 goals 1 goal

Memory usage!

(4)

sumlist([], 0). sumlist([H|T], Sum) :-sumlist(T, TmpSum), Sum is TmpSum + H. sumlist(List, Sum) :-sumlist_acc(List, Sum, 0).

% sumlist_acc(List, OutputSum, Accumulator)

sumlist_acc([], Sum, Sum).

sumlist_acc([H|T], Sum, AccSum) :-NewAccSum is AccSum + H,

sumlist_acc(T, Sum, NewAccSum).

Make it tail-recursive by introducing

accumulator:

(5)

sumlist(List, Sum)

:-sumlist_acc(List, Sum, 0). sumlist_acc([], Sum, Sum).

sumlist_acc([H|T], Sum, AccSum) :-NewAccSum is AccSum + H,

sumlist_acc(T, Sum, NewAccSum). ?- sumlist([5,4,9,8], S). ?- sumlist_acc([5,4,9,8], S, 0). ?- NewAccS1 is 0 + 5, sumlist_acc([4,9,8], S, NewAccS1).

1 goal

2 goals

?- sumlist_acc([4,9,8],S, 5). ?- NewAccS2 is 5 + 4, sumlist_acc([9,8], S, NewAccS2). ?- sumlist_acc([9,8], S, 9). ?- NewAccS3 is 9 + 9, sumlist_acc([8], S, NewAccS3). ?- sumlist_acc([8], S, 18). ?- NewAccS4 is 18 + 8, sumlist_acc([], S, NewAccS4).

?- sumlist_acc([], S, 26). 1 goal 2 goals 1 goal 1 goal 1 goal 2 goals 2 goals

(6)

Reverse without accumulator:

reverse([], []).

reverse([Head|Tail], Reversed):-reverse(Tail, TailReversed),

append(TailReversed, [Head], Reversed).

Reverse with accumulator:

reverse(List,

Reversed):-reverse_acc(List, Reversed, []). reverse_acc([], Result, Result).

reverse_acc([Head|Tail], Result,

Acc):-reverse_acc(Tail, Result, [Head|Acc]).

Possible exercise: draw a call tree for

?- reverse([1, 2, 3], Reversed).

for both versions

quadratic time-complexity: O(N2)

(7)
(8)

list_length_2(List, Length)

:-list_length_acc(List, Length, 0).

list_length_acc([], Length, Length).

list_length_acc([ _Head|Tail], Length,

Acc):-Acc1 is Acc +1,

list_length_acc(Tail, Length, Acc1).

Draw a call tree for

?- list_length([a, b, c], Length)

(without accumulator)

and for (with and without accumulator)

?- list_length_2([a, b, c], Length)

(with accumulator)

(9)

L = [5, 3, 7, 8, 1, 4, 7, 6] Tail = [3, 7, 8, 1, 4, 7, 6] Small = [3, 1, 4] Big = [7, 8, 7, 6] SortedSmall = [1, 3, 4] SortedBig = [6, 7, 7, 8] Sorted = [1, 3, 4, 5, 6, 7, 7, 8] Delete X, X = 5 Quicksort of Big Quicksort of Small Quicksort of L Split All <= X All > X Add X concatenate

1.2 Quicksort

(10)

quicksort_2(List, Sorted)

:-quicksort_acc(List, [], Sorted).

quicksort_acc([], Sorted, Sorted).

quicksort_acc([X|Tail], Acc, Sorted)

:-split(X, Tail, Small, Big),

quicksort_acc(Big, Acc, NewAcc),

quicksort_acc(Small, [X|NewAcc], Sorted).

Intuition: in

quicksort_acc(List, Acc, Sorted) :

• Acc is a sorted list of elements larger than or equal to the

elements in List.

• Sorted is a list:

• Starting with the sorted elements of List

• Followed by the elements from Acc

(11)

1.3 Computing Fibonacci numbers

From exercise session 2:

fib(1,1).

fib(2,1).

fib(N, F)

:-N > 2,

N1 is N-1,

fib(N1, F1),

N2 is N-2,

fib(N2, F2),

F is F1 + F2.

(12)

fibo(N, Fib)

:-fib(1 ,N, 0, 1, Fib).

fib(N, N, _, Result, Result).

fib(Count, N, PreviousFib, CurrentFib,

Result):-Count < N,

NextCount is Count + 1,

NextFib is CurrentFib + PreviousFib,

fib(NextCount, N, CurrentFib, NextFib, Result).

1st argument: which step we are in

2nd argument: which fibonacci number we eventually want to compute 3th argument: the fibonacci number for the previous step

4th argument: the fibonacci number for the current step

5th argument: where we will return the solution when the recursion has finished.

(13)

get_doubles(List, Doubles)

:-get_doubles_acc(List, Doubles, [], []).

get_doubles_acc([], Doubles, _, Doubles).

get_doubles_acc([El|T], Result, Singles,

Doubles):-% we have seen the element El twice (or more) before member(El, Doubles), !,

get_doubles_acc(T, Result, Singles, Doubles). get_doubles_acc([El|T], Result, Singles,

Doubles):-% we have seen the element El once before

member(El, Singles), remove(El, Singles, NewSingles), !, get_doubles_acc(T, Result, NewSingles, [El | Doubles]). get_doubles_acc([El|T] ,Result, Singles,

Doubles):-% we have not seen the element El before

get_doubles_acc(T, Result, [El | Singles], Doubles).

(14)

arc(From, To, Graph) :-member(From/To, Graph). connected(X ,X, _). connected(X, Y, Graph) :-arc(Z, Y, Graph), connected(X, Z, Graph).

(15)

find_path(X, Y, Path, Graph)

:-find_path_acc(X, Y, Path, [Y], Graph). find_path_acc(X, X, Path, Path, _).

find_path_acc(X, Y, Path, Acc, G) :-arc(Z, Y, G),

find_path_acc(X, Z, Path, [Z|Acc], G).

(16)

find_path_2(X, Y, Path, Graph)

:-find_path_acc_2(X, Y, Path, [Y], Graph). find_path_acc_2(X ,X, Path, Path, _).

find_path_acc_2(X, Y, Path, Acc, G) :-arc(Z, Y, G),

not(member(Z, Acc)), % <--- only difference

find_path_acc_2(X, Z, Path, [Z| Acc], G). connected_2(X, Y,

Graph):-find_path_2(X, Y, _, Graph).

(17)

arc(From,To,Graph)

:-member(From/To,Graph). arc(From,To,Graph)

:-member(To/From,Graph).

References

Related documents

Kriger, ‘ ‘‘Guinea Cloth’’: Production and Consumption of Cotton Textiles in West Africa before and during the Atlantic Slave Trade’, in Giorgio Riello and Prasannan

The objectives of the research study are to study non-corroded and corroded reinforced concrete beams using finite element analysis to understand the response of non corroded

The most common instance of muscle entrapment occurs when the inferior rectus muscle becomes entrapped in a trapdoor fracture of the orbital floor (Figure 8) (Helveston,

For example, in [10] it was shown that 1, 2, and 3 are the only Fibonacci numbers whose Euler function is also a Fibonacci number, while in [3] it was proved that if {u n } n≥0 is

(Trimax) is a leading end-to-end service provider of IT Services and Solutions encompassing Integration, Data Centre, Management of IT Infrastructure, and Networking.  Firm has

DP technique The n-th Fibonacci number Guideline W activity selection 0-1 Knapsack..

It is therefore strongly recommended to perform small- scale trial cleavage runs with lo-50 mg of peptide resin and different cleavage cocktails, as well as variations

Your hand creates a golden section in relation to your arm, as the ratio of your forearm to your hand is also 1.618, the Divine Proportion. THE