SHARE
TWEET

Untitled

a guest Mar 25th, 2019 91 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. hello :- format('Hello world~n').
  2.  
  3. prod_list([],Product) :-
  4.     Product is 1,
  5.     !.
  6. prod_list(List,Product) :-
  7.     List = [H|T],
  8.     prod_list(T,Right),
  9.     Product is H * Right.
  10.  
  11. palindrome([]).
  12. palindrome([_]).
  13. palindrome(List) :-
  14.     append([H|T],[H],List),
  15.     palindrome(T),!.
  16.  
  17.  
  18. minimal_mirror(List, Result) :-
  19.     palindrome(List) ->
  20.         Result = List;
  21.         List = [H|T],
  22.         minimal_mirror(T,Rest),
  23.         append([H],Rest,ResultList),
  24.         append(ResultList,[H],Final),
  25.         Result = Final.
  26.  
  27.  
  28. checkList([]) :-
  29.     !.
  30.  
  31. checkList([H|T]) :-
  32.     number(H) ->
  33.         checkList(T);
  34.     format('ERROR: "'),
  35.     write(H),
  36.     format('" is not a number~n'),
  37.     false,
  38.     !.
  39.  
  40.  
  41. secondMin(List,Min) :-
  42.     not(checkList(List)) ->
  43.     true;
  44.     sort(List,NewList),
  45.     length(NewList, L),
  46.     L > 1 ->
  47.        NewList = [_,N|_],
  48.        Min is N;
  49.     format('ERROR: List has fewer than two distinct elements~n').
  50.  
  51. classify(_,[],[],[]) :-
  52.     !.
  53. classify(Funt,[H|T],[H|T2],False) :-
  54.     call(Funt,H),
  55.     classify(Funt,T,T2,False),
  56.     !.
  57. classify(Funt,[H|T],True,[H|T2]) :-
  58.     classify(Funt,T,True,T2),
  59.     !.
  60.  
  61. prefix([],_) :-
  62.     !.
  63. prefix([H1|T1],[H2|T2]) :-
  64.     H1 == H2,
  65.     prefix(T1,T2),
  66.     !.
  67.  
  68. suffix(Last,Last) :-
  69.     !.
  70. suffix(Suffix,[_|T]) :-
  71.     suffix(Suffix,T),
  72.     !.
  73.  
  74.  
  75. bookends(Prefix,Suffix,List) :-
  76.     prefix(Prefix,List),
  77.     suffix(Suffix,List).
  78.  
  79.  
  80. subslice([],_) :-
  81.     !.
  82. subslice(List,[H|T]) :-
  83.     prefix(List,[H|T])->
  84.     !;
  85.     subslice(List,T),
  86.     !.
  87.  
  88. shift(List1,N,Result) :-
  89.     length(List1,L),
  90.     N1 is N mod L,
  91.     N1 < 0, !,
  92.     N2 is L + N1,
  93.     shift(List1,N2,Result),
  94.     !.
  95.  
  96. shift(List1,N,Result) :-
  97.     length(List1, L),
  98.     N1 is N mod L,
  99.     append(Left,Right,List1),
  100.     append(Right,Left,Result),
  101.     length(Left,N1),
  102.     !.
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110. luhn(Number) :-
  111.    makeList(Number,List),
  112.    doubleEveryOther(List,Double),
  113.    sumDigits(Double,Result),
  114.    Final is Result mod 10,
  115.    Final is 0.
  116.  
  117. makeList(0,[]).
  118. makeList(Number,List) :-
  119.     Digit is Number mod 10,
  120.     Rest is floor(Number/10),
  121.     makeList(Rest, NewList),
  122.     List = [Digit|NewList],
  123.     !.
  124. doubleEveryOther([],NewList) :-
  125.     NewList = [],
  126.     !.
  127. doubleEveryOther([OnlyElement], NewList) :-
  128.     NewList = [OnlyElement],
  129.     !.
  130. doubleEveryOther([First,Second|Rest],[Clone,Calculate|Double]) :-
  131.     Clone is First,
  132.     Calculate is Second * 2,
  133.     doubleEveryOther(Rest,Double).
  134.  
  135. sumDigits([],0).
  136. sumDigits(List, Result) :-
  137.     List = [H|T],
  138.     sumDigits(T,RestResult),
  139.     FirstDigit is H mod 10,
  140.     SecondDigit is floor(H/10),
  141.     Result is FirstDigit + SecondDigit + RestResult,
  142.     !.
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top