Share Pastebin
Guest
Public paste!

Snaut

By: a guest | Feb 9th, 2010 | Syntax: Erlang | Size: 4.30 KB | Hits: 44 | Expires: Never
Copy text to clipboard
  1. -module(hw1).
  2.  
  3. -compile(export_all).
  4.  
  5.  
  6.  
  7. max([]) -> [];
  8.  
  9. max([X]) -> X;
  10.  
  11. max([A|[B|T]]) -> case A>B of
  12.  
  13.                                         true -> max([A|T]);
  14.  
  15.                                         false -> max([B|T])
  16.  
  17.                         end.
  18.  
  19.  
  20.  
  21. min([]) -> [];
  22.  
  23. min([X]) -> X;
  24.  
  25. min([A|[B|T]]) -> case A<B of
  26.  
  27.                                         true -> min([A|T]);
  28.  
  29.                                         false -> min([B|T])
  30.  
  31.                         end.
  32.  
  33.  
  34.  
  35.  
  36.  
  37. min_max([]) -> [];
  38.  
  39. min_max(A) -> {min(A),max(A)}.
  40.  
  41.  
  42.  
  43. is_palindrome([])->true;
  44.  
  45. is_palindrome(A)-> case lists:reverse(A)==A of
  46.  
  47.                                         true when is_list(A) -> true;
  48.  
  49.                                         false -> false
  50.  
  51.                                 end.
  52.  
  53.  
  54.  
  55. product([])->0;
  56.  
  57. product([A])->A;
  58.  
  59. product([A|[B|T]])->product([A*B|T]).
  60.  
  61.  
  62.  
  63. all_different(A,B,C)-> (A/=B) and (A/=C) and (B/=C).
  64.  
  65.  
  66.  
  67. is_proper_list([])->true;
  68. is_proper_list([_])->true;
  69.  
  70. is_proper_list([_|T])->case is_list(T) of
  71.  
  72.                                         true->is_proper_list(T);
  73.  
  74.                                         false->false
  75.  
  76.                                 end.
  77.  
  78. cutlen(A,X) -> move_(A,[],X).
  79.  
  80. slice(A,B,[H|T]) -> case (A>1) of
  81.                                         true -> slice(A-1,B-1,T);
  82.                                         false -> cutlen(B,[H|T])
  83.                                 end.
  84.  
  85.  
  86. move_(0,Acc,_) -> lists:reverse(Acc);
  87. move_(A,Acc,[H|T])->move_(A-1,[H|Acc],T).
  88.  
  89. max(X,Y,Z) ->
  90.  
  91.         if
  92.  
  93.                 X >= Y andalso X >= Z -> X;
  94.  
  95.                 Y >= X andalso Y >= Z -> Y;
  96.  
  97.                 true -> Z
  98.  
  99.         end.
  100.  
  101. num_solutions(A,B,C) -> D=B*B-4*A*C,
  102.                                         if D>0 -> 2;
  103.                                                 D==0 -> 1;
  104.                                                 true -> 0
  105.                                         end.
  106.  
  107. after_tax(Income) -> if Income=<50000 -> Income;
  108.                                                 Income=<70000 -> (Income-50000)*0.9+after_tax(50000);
  109.                                                 Income=<90000 -> (Income-70000)*0.8+after_tax(70000);
  110.                                                 Income=<110000 -> (Income-90000)*0.7+after_tax(90000);
  111.                                                         true -> (Income-110000)*0.6+after_tax(110000)
  112.                                         end.
  113.  
  114. reverse_list_(B,[]) -> B;
  115. reverse_list_(B,[H|T]) -> reverse_list_([H|B],T).
  116.  
  117. reverse_list(A) -> reverse_list_([],A).
  118.  
  119. flatten([]) -> [];
  120. flatten([A]) -> case is_list(A) of
  121.                                 true -> flatten(A);
  122.                                 false -> [A]
  123.                         end;
  124. flatten([H|T]) -> case is_list(H) of
  125.                                         true -> flatten(H);
  126.                                         false -> [H]
  127.                                 end
  128.                         ++
  129.                                 case is_list(T) of
  130.                                         true -> flatten(T);
  131.                                         false -> [T]
  132.                                 end;
  133. flatten(A) -> A.
  134.  
  135. fast_power_(_,_,0) -> 1;
  136. fast_power_(_,X,1) -> X;
  137. fast_power_(B,X,N) -> fast_power_(B,X*B,N-1).
  138.  
  139. fast_power(0, 0) -> error;
  140. fast_power(X, N) -> case is_integer(N) of
  141.                                                 true -> fast_power_(X,X,N);
  142.                                                 false -> error
  143.                                         end.
  144.  
  145. %weekday(Day, Month, Year) -> case (calendar:date_to_gregorian_days
  146. %                                                               (Year,
  147. %                                                               if Month == january -> 1;
  148. %                                                                       Month == february -> 2;
  149. %                                                                       Month == march -> 3;
  150. %                                                                       Month == april -> 4;
  151. %                                                                       Month == may -> 5;
  152. %                                                                       Month == june -> 6;
  153. %                                                                       Month == jule -> 7;
  154. %                                                                       Month == august -> 8;
  155. %                                                                       Month == september -> 9;
  156. %                                                                       Month == october -> 10;
  157. %                                                                       Month == november -> 11;
  158. %                                                                       Month == december -> 12
  159. %                                                               end,                                                                   
  160. %                                                               Day) - 1) rem 7 of
  161. %                                                       1 -> monday;
  162. %                                                       2 -> tuesday;
  163. %                                                       3 -> wednesday;
  164. %                                                       4 -> thursday;
  165. %                                                       5 -> friday;
  166. %                                                       6 -> saturday;
  167. %                                                       0 -> sunday
  168. %                                                       end.
  169.  
  170. weekday(Day, Month, Year) -> case calendar:day_of_the_week(Year,
  171.                                                                 if Month == january -> 1;
  172.                                                                         Month == february -> 2;
  173.                                                                         Month == march -> 3;
  174.                                                                         Month == april -> 4;
  175.                                                                         Month == may -> 5;
  176.                                                                         Month == june -> 6;
  177.                                                                         Month == jule -> 7;
  178.                                                                         Month == august -> 8;
  179.                                                                         Month == september -> 9;
  180.                                                                         Month == october -> 10;
  181.                                                                         Month == november -> 11;
  182.                                                                         Month == december -> 12
  183.                                                                 end,                                                                   
  184.                                                                 Day) of
  185.                                                         1 -> monday;
  186.                                                         2 -> tuesday;
  187.                                                         3 -> wednesday;
  188.                                                         4 -> thursday;
  189.                                                         5 -> friday;
  190.                                                         6 -> saturday;
  191.                                                         7 -> sunday
  192.                                                         end.
  193.  
  194. rle([])->[];
  195. rle([A])->case is_tuple(A) of
  196.                         true -> A;
  197.                         false -> [{A,1}]
  198.                 end;
  199. rle([{H1,C}|[H1]])->[{H1,C+1}];
  200. rle([{H1,C}|[H2]])->[{H1,C}|rle([H2])];
  201. rle([{H1,C}|[H1|T]])->rle([{H1,C+1}|T]);
  202. rle([{H1,C}|[H2|T]])->[{H1,C}|rle([H2|T])];
  203. rle([H|T])->rle([{H,1}|T]).
  204.  
  205. rle1([])->[];
  206. rle1([A])->[A];
  207. rle1([{H1,C}|[H1]])->[{H1,C+1}];
  208. rle1([{H1,C}|[H2]])->[{H1,C}|rle1([H2])];
  209. rle1([{H1,C}|[H1|T]])->rle1([{H1,C+1}|T]);
  210. rle1([{H1,C}|[H2|T]])->[{H1,C}|rle1([H2|T])];
  211. rle1([H1|[H1]])->rle1([{H1,2}]);
  212. rle1([H1|[H1|T]])->rle1([{H1,2}|T]);
  213. rle1([H|T])->[H|rle1(T)].
  214.  
  215. rld([])->[];
  216. rld([{X,1}])->[X];
  217. rld([{X,C}])->if is_integer(C) and (C > 1) -> [X|rld([{X,C-1}])] end;
  218. rld([A])-> [A];
  219. rld([H|T])->rld([H])++rld(T).