Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %%%-------------------------------------------------------------------
- %%% @author Kuba
- %%% @copyright (C) 2017, <COMPANY>
- %%% @doc
- %%%
- %%% @end
- %%% Created : 17. paź 2017 13:08
- %%%-------------------------------------------------------------------
- -module(main).
- -author("Jakub Jańczak-Zarycki").
- %% API
- -export([beach/1]).
- -export([help_me/1]).
- -export([run/1]).
- -export([tail_duplicate/2]).
- -export([tail_duplicate/3]).
- -export([valid_time/1]).
- -export([insert_2/2]).
- -export([valid_time_2/1]).
- -export([insert/2]).
- -export([dolphin1/1]).
- -export([member/2]).
- -export([qsort/1]).
- -export([map/2]).
- -export([a/0]).
- -export([iterate/3]).
- -export([b/1]).
- -export([member/1]).
- -export([member_2/2]).
- -export([insert_3/2]).
- -export([base1/0]).
- -export([base2/0]).
- -export([filter/2]).
- -export([loop/0]).
- -export([go/1]).
- -compile(export_all).
- % Operation list
- beach(Temperature) ->
- case Temperature of
- {celsius, N} when N >= 20, N =< 45 ->
- 'favorable';
- {kelvin, N} when N >= 293, N =< 318 ->
- 'scientifically favorable';
- {fahrenheit, N} when N >= 68, N =< 113 ->
- 'favorable in the US';
- _ ->
- 'avoid beach'
- end.
- help_me(Animal) ->
- Talk = if Animal == cat -> "meow";
- Animal == beef -> "mooo";
- Animal == dog -> "bark";
- Animal == tree -> "bark";
- true -> "fgdadfgna"
- end,
- {Animal, "says " ++ Talk ++ "!"}.
- valid_time({Date = {Y,M,D}, Time = {H,Min,S}}) ->
- io:format("The Date tuple (~p) says today is: ~p/~p/~p,~n",[Date,Y,M,D]),
- io:format("The time tuple (~p) indicates: ~p:~p:~p.~n", [Time,H,Min,S]);
- valid_time(_) ->
- io:format("Stop feeding me wrong data!~n").
- valid_time_2(DateTime) ->
- case DateTime of
- {Date = {Y,M,D}, Time = {H,Min,S}} ->
- io:format("The Date tuple (~p) says today is: ~p/~p/~p,~n",[Date,Y,M,D]),
- io:format("The time tuple (~p) indicates: ~p:~p:~p.~n", [Time,H,Min,S]);
- _ -> io:format("Stop feeding me wrong data!~n")
- end.
- insert(X,[ ]) ->
- [X];
- insert(X,Set) ->
- Result = case lists:member(X,Set) of
- true -> Set;
- false -> [X|Set]
- end,
- {"This is result",Result}.
- tail_duplicate(N,Term) ->
- tail_duplicate(N,Term,[]).
- tail_duplicate(0,_,List) ->
- List;
- tail_duplicate(N,Term,List) when N > 0 ->
- tail_duplicate(N-1, Term, [Term|List]).
- member(X,Set)->
- Result = [Lp || Lp<-Set, Lp == X],
- member(Result).
- member([_])->
- true;
- member(_)->
- false.
- check_member(X,Head)->
- X==Head.
- member_2(X,[H|T])->
- case X of
- H -> true;
- _ -> member_2(X, T)
- end;
- member_2(_,[])->
- false.
- insert_2(X,Set)->
- qsort([X|Set]).
- insert_3(X,Set)->
- [E || E <- Set, E<X ] ++ [X] ++ [E1 || E1 <-Set, E1>=X].
- qsort([H|T]) ->
- qsort([E || E <- T, E < H]) ++
- [H] ++
- qsort([E || E <- T, E >= H]);
- qsort([]) ->
- [].
- base1() -> A = 1, (fun() -> A = 2 end)(), A.
- base2() -> A = 1, (fun(A) -> A = 2 end)(2), A.
- a() -> Secret = "pony", (fun() -> Secret end).
- b(F) -> "a/0's password is "++F().
- map(_, []) -> [];
- map(F, [H|T]) -> [F(H)|map(F,T)].
- filter(P,[ ]) -> [ ];
- filter(P,[H|T]) ->
- case P(H) of
- true -> [H| filter(P,T)];
- _ -> filter(P,T)
- end.
- iterate(S, IsDone, Transform) ->
- C = IsDone(S),
- if C -> S;
- true -> S1 = Transform(S), iterate(S1, IsDone, Transform)
- end.
- dolphin1(IsString) ->
- receive
- do_a_flip ->
- io:format("How about no?~s~n",[IsString]),
- dolphin1(IsString);
- fish ->
- io:format("So long and thanks for all the fish!~n"),
- dolphin1(IsString);
- IsString->
- io:format("Yolo~n"),
- dolphin1(IsString);
- _ ->
- io:format("Heh, we're smarter than you humans.~n"),
- dolphin1(IsString)
- end.
- loop() ->
- io:format("Try loop~n"),
- receive
- {From, Msg} ->
- From ! {self(), Msg},
- loop();
- stop ->
- true;
- {error, From, Msg} ->
- io:fwrite("Error: ~s~n", [Msg]),
- From ! error,
- true
- end.
- go(Option) ->
- Pid2 = spawn(main, loop, []),
- case Option of
- 1->
- Pid2 ! {self(), hello};
- _->
- Pid2 ! {error, self(), "Error message"}
- end,
- receive
- {Pid2, Msg} ->
- io:format("P1 ~w~n",[Msg]);
- error ->
- io:format("Error~n")
- end,
- Pid2 ! stop.
- run(Option) ->
- case Option of
- 1 -> Talk = help_me(cat), {Option, element(2,Talk)};
- 2 -> insert(4,[1,2,3]);
- 3 -> insert(2,[1,2]);
- 4 -> main:filter((fun(X)-> X rem 2 == 0 end),[1,2,5,6,1]);
- _ -> help_me("")
- end.
- %[3,4]++[3,8,9]
- %[5,6,7,8]- -[6,8]
- %[1,2,3]- -[1,3]- -[2]
- %[1,2,3]- -[1,3]- -[1]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement