% swipl --goal=main -c aula.pl
% Gera um arquivo a.out

% parent(Parent, Child)
parent(albert, jim).
parent(albert, peter).
parent(pat, jim).
parent(amanda, jenny).
parent(jenny, peter).

female(pat).
female(jenny).
female(amanda).

male(albert).
male(jim).
male(peter).


grandparent(Grandparent, Grandchild) :-
    parent(Grandparent, Child),
    parent(Child, Grandchild).

cons(L1, L2, Result) :-
	Result = [L1 | L2].

is_member(Element, [Element | _]).
is_member(Element, [_ | Tail]) :-
	is_member(Element,Tail).


fatorial(0, 1).
fatorial(N, Valor) :- 
    N1 is N - 1, fatorial(N1, Anterior), Valor is Anterior * N.

map(_, [], []).
map(Predicado, [H | T], [N | NS]) :-
    call(Predicado, H, N),
    map(Predicado, T, NS).

get_last_element([], Element) :- Element = [].
get_last_element([ _ | Tail], Element) :-
	Tail \== [], get_last_element(Tail,Element).
get_last_element([Head | Tail], Element) :-
	Tail == [], Element = [Head].


% Calcula o log da cabeça e "retorna" uma List [Head, Log]
get_log(Head, List) :-
	Log is log(Head),
	List = [Head,Log].

log_table([], ResultList) :- ResultList = [].
log_table( [ Head | Tail ], ResultList) :-
	get_log(Head,HeadLog),
	log_table(Tail, L),
	ResultList = [ HeadLog | L ].

fibonacci(0, 1).
fibonacci(1, 1).
fibonacci(N, V) :- N > 1, N1 is N - 1, N2 is N - 2, fibonacci(N1,V1), fibonacci(N2,V2), V is V1 + V2.

anoBissexto(Y) :-
    Y mod 4 =:= 0,
    Y mod 100 =:= 0,
    Y mod 400 =:= 0.
anoBissexto(Y) :-
    Y mod 4 =:= 0,
    Y mod 100 =\= 0.

bissextos(Ys) :-
    findall(X, (between(1584, 2020, X), anoBissexto(X) ), Ys).


manhattan_distance([], [], D) :- D is 0.
manhattan_distance([H1|T1], [H2|T2], D) :-
    Signed is (H1 - H2),
    abs(Signed, Unsigned),
    manhattan_distance(T1, T2, NovoD),
    D is NovoD + Unsigned.


    
    
main :-
    write("manhattan\n"),
    manhattan_distance([2,1], [2,1], R),
    write(R).
