Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Erlang 4.61 KB | None | 0 0
  1. %%%-------------------------------------------------------------------
  2. %%% @author Kuba
  3. %%% @copyright (C) 2017, <COMPANY>
  4. %%% @doc
  5. %%%
  6. %%% @end
  7. %%% Created : 17. paź 2017 13:08
  8. %%%-------------------------------------------------------------------
  9. -module(main).
  10. -author("Jakub Jańczak-Zarycki").
  11.  
  12. %% API
  13. -export([beach/1]).
  14. -export([help_me/1]).
  15. -export([run/1]).
  16. -export([tail_duplicate/2]).
  17. -export([tail_duplicate/3]).
  18. -export([valid_time/1]).
  19. -export([insert_2/2]).
  20. -export([valid_time_2/1]).
  21. -export([insert/2]).
  22. -export([dolphin1/1]).
  23. -export([member/2]).
  24. -export([qsort/1]).
  25. -export([map/2]).
  26. -export([a/0]).
  27. -export([iterate/3]).
  28. -export([b/1]).
  29. -export([member/1]).
  30. -export([member_2/2]).
  31. -export([insert_3/2]).
  32. -export([base1/0]).
  33. -export([base2/0]).
  34. -export([filter/2]).
  35. -export([loop/0]).
  36. -export([go/1]).
  37. -compile(export_all).
  38. % Operation list
  39.  
  40. beach(Temperature) ->
  41.   case Temperature of
  42.     {celsius, N} when N >= 20, N =< 45 ->
  43.       'favorable';
  44.     {kelvin, N} when N >= 293, N =< 318 ->
  45.       'scientifically favorable';
  46.     {fahrenheit, N} when N >= 68, N =< 113 ->
  47.       'favorable in the US';
  48.     _ ->
  49.       'avoid beach'
  50.   end.
  51.  
  52. help_me(Animal) ->
  53.   Talk = if Animal == cat  -> "meow";
  54.            Animal == beef -> "mooo";
  55.            Animal == dog  -> "bark";
  56.            Animal == tree -> "bark";
  57.            true -> "fgdadfgna"
  58.          end,
  59.   {Animal, "says " ++ Talk ++ "!"}.
  60.  
  61. valid_time({Date = {Y,M,D}, Time = {H,Min,S}}) ->
  62.   io:format("The Date tuple (~p) says today is: ~p/~p/~p,~n",[Date,Y,M,D]),
  63.   io:format("The time tuple (~p) indicates: ~p:~p:~p.~n", [Time,H,Min,S]);
  64. valid_time(_) ->
  65.   io:format("Stop feeding me wrong data!~n").
  66.  
  67. valid_time_2(DateTime) ->
  68.   case DateTime of
  69.     {Date = {Y,M,D}, Time = {H,Min,S}} ->
  70.       io:format("The Date tuple (~p) says today is: ~p/~p/~p,~n",[Date,Y,M,D]),
  71.       io:format("The time tuple (~p) indicates: ~p:~p:~p.~n", [Time,H,Min,S]);
  72.     _ ->   io:format("Stop feeding me wrong data!~n")
  73. end.
  74.  
  75. insert(X,[ ]) ->
  76.   [X];
  77. insert(X,Set) ->
  78.   Result = case lists:member(X,Set) of
  79.     true  ->  Set;
  80.     false -> [X|Set]
  81.   end,
  82.   {"This is result",Result}.
  83.  
  84. tail_duplicate(N,Term) ->
  85.   tail_duplicate(N,Term,[]).
  86. tail_duplicate(0,_,List) ->
  87.   List;
  88. tail_duplicate(N,Term,List) when N > 0 ->
  89.   tail_duplicate(N-1, Term, [Term|List]).
  90.  
  91.  
  92. member(X,Set)->
  93.   Result = [Lp || Lp<-Set, Lp == X],
  94.   member(Result).
  95. member([_])->
  96.   true;
  97. member(_)->
  98.   false.
  99.  
  100. check_member(X,Head)->
  101.   X==Head.
  102.  
  103. member_2(X,[H|T])->
  104.   case X of
  105.     H -> true;
  106.     _ -> member_2(X, T)
  107.   end;
  108. member_2(_,[])->
  109.   false.
  110.  
  111. insert_2(X,Set)->
  112.   qsort([X|Set]).
  113.  
  114. insert_3(X,Set)->
  115.   [E || E <- Set, E<X ] ++ [X] ++ [E1 || E1 <-Set, E1>=X].
  116.  
  117.  
  118. qsort([H|T]) ->
  119.   qsort([E || E <- T, E < H]) ++
  120.     [H] ++
  121.     qsort([E || E <- T, E >= H]);
  122. qsort([]) ->
  123.   [].
  124.  
  125. base1() -> A = 1, (fun() -> A = 2 end)(), A.
  126. base2() -> A = 1, (fun(A) -> A = 2 end)(2), A.
  127.  
  128. a() -> Secret = "pony", (fun() -> Secret end).
  129. b(F) -> "a/0's password is "++F().
  130.  
  131. map(_, []) -> [];
  132. map(F, [H|T]) -> [F(H)|map(F,T)].
  133.  
  134. filter(P,[ ]) -> [ ];
  135. filter(P,[H|T]) ->
  136.   case P(H) of
  137.     true -> [H| filter(P,T)];
  138.     _ -> filter(P,T)
  139.   end.
  140.  
  141. iterate(S, IsDone, Transform) ->
  142.   C = IsDone(S),
  143.   if C -> S;
  144.     true -> S1 = Transform(S), iterate(S1, IsDone, Transform)
  145.   end.
  146.  
  147. dolphin1(IsString) ->
  148.   receive
  149.     do_a_flip ->
  150.       io:format("How about no?~s~n",[IsString]),
  151.       dolphin1(IsString);
  152.     fish ->
  153.       io:format("So long and thanks for all the fish!~n"),
  154.       dolphin1(IsString);
  155.     IsString->
  156.       io:format("Yolo~n"),
  157.       dolphin1(IsString);
  158.     _ ->
  159.       io:format("Heh, we're smarter than you humans.~n"),
  160.       dolphin1(IsString)
  161.   end.
  162.  
  163. loop() ->
  164.   io:format("Try loop~n"),
  165.   receive
  166.     {From, Msg} ->
  167.       From ! {self(), Msg},
  168.       loop();
  169.     stop ->
  170.       true;
  171.     {error, From, Msg} ->
  172.       io:fwrite("Error: ~s~n", [Msg]),
  173.       From ! error,
  174.       true
  175.   end.
  176.  
  177.  
  178. go(Option) ->
  179.   Pid2 = spawn(main, loop, []),
  180.   case Option of
  181.     1->
  182.       Pid2 ! {self(), hello};
  183.     _->
  184.       Pid2 ! {error, self(), "Error message"}
  185.   end,
  186.  
  187.   receive
  188.     {Pid2, Msg} ->
  189.       io:format("P1 ~w~n",[Msg]);
  190.     error ->
  191.       io:format("Error~n")
  192.   end,
  193.  
  194.   Pid2 ! stop.
  195.  
  196.  
  197.  
  198.  
  199.  
  200. run(Option) ->
  201.   case Option of
  202.     1 -> Talk = help_me(cat), {Option, element(2,Talk)};
  203.     2 -> insert(4,[1,2,3]);
  204.     3 -> insert(2,[1,2]);
  205.     4 -> main:filter((fun(X)->  X rem 2 == 0 end),[1,2,5,6,1]);
  206.     _ -> help_me("")
  207.   end.
  208.  
  209. %[3,4]++[3,8,9]
  210. %[5,6,7,8]- -[6,8]
  211. %[1,2,3]- -[1,3]- -[2]
  212. %[1,2,3]- -[1,3]- -[1]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement