Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- hello :- format('Hello world~n').
- prod_list([],Product) :-
- Product is 1,
- !.
- prod_list(List,Product) :-
- List = [H|T],
- prod_list(T,Right),
- Product is H * Right.
- palindrome([]).
- palindrome([_]).
- palindrome(List) :-
- append([H|T],[H],List),
- palindrome(T),!.
- minimal_mirror(List, Result) :-
- palindrome(List) ->
- Result = List;
- List = [H|T],
- minimal_mirror(T,Rest),
- append([H],Rest,ResultList),
- append(ResultList,[H],Final),
- Result = Final.
- checkList([]) :-
- !.
- checkList([H|T]) :-
- number(H) ->
- checkList(T);
- format('ERROR: "'),
- write(H),
- format('" is not a number~n'),
- false,
- !.
- secondMin(List,Min) :-
- not(checkList(List)) ->
- true;
- sort(List,NewList),
- length(NewList, L),
- L > 1 ->
- NewList = [_,N|_],
- Min is N;
- format('ERROR: List has fewer than two distinct elements~n').
- classify(_,[],[],[]) :-
- !.
- classify(Funt,[H|T],[H|T2],False) :-
- call(Funt,H),
- classify(Funt,T,T2,False),
- !.
- classify(Funt,[H|T],True,[H|T2]) :-
- classify(Funt,T,True,T2),
- !.
- prefix([],_) :-
- !.
- prefix([H1|T1],[H2|T2]) :-
- H1 == H2,
- prefix(T1,T2),
- !.
- suffix(Last,Last) :-
- !.
- suffix(Suffix,[_|T]) :-
- suffix(Suffix,T),
- !.
- bookends(Prefix,Suffix,List) :-
- prefix(Prefix,List),
- suffix(Suffix,List).
- subslice([],_) :-
- !.
- subslice(List,[H|T]) :-
- prefix(List,[H|T])->
- !;
- subslice(List,T),
- !.
- shift(List1,N,Result) :-
- length(List1,L),
- N1 is N mod L,
- N1 < 0, !,
- N2 is L + N1,
- shift(List1,N2,Result),
- !.
- shift(List1,N,Result) :-
- length(List1, L),
- N1 is N mod L,
- append(Left,Right,List1),
- append(Right,Left,Result),
- length(Left,N1),
- !.
- luhn(Number) :-
- makeList(Number,List),
- doubleEveryOther(List,Double),
- sumDigits(Double,Result),
- Final is Result mod 10,
- Final is 0.
- makeList(0,[]).
- makeList(Number,List) :-
- Digit is Number mod 10,
- Rest is floor(Number/10),
- makeList(Rest, NewList),
- List = [Digit|NewList],
- !.
- doubleEveryOther([],NewList) :-
- NewList = [],
- !.
- doubleEveryOther([OnlyElement], NewList) :-
- NewList = [OnlyElement],
- !.
- doubleEveryOther([First,Second|Rest],[Clone,Calculate|Double]) :-
- Clone is First,
- Calculate is Second * 2,
- doubleEveryOther(Rest,Double).
- sumDigits([],0).
- sumDigits(List, Result) :-
- List = [H|T],
- sumDigits(T,RestResult),
- FirstDigit is H mod 10,
- SecondDigit is floor(H/10),
- Result is FirstDigit + SecondDigit + RestResult,
- !.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement