Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

mrman2

% hello world program
-module(helloworld).
-export([start/0]).

calculate(N) ->
    N + 2.

start() ->
    io:format("~p", [spin(lists:seq(1, 10), fun calculate/1)]).

spin(List, Fun) ->
    reel([wrap(Fun, Ele) || Ele <- List]).
    
reel([H | T]) ->
    receive
        {H, Value} -> [Value]
    end ++ reel(T);
reel([]) ->
    [].
    
wrap(F, Ele) ->
    ID = self(),
    spawn(fun() ->
        ID ! {self(), F(Ele)}
    end).

mrman

% hello world program
-module(helloworld).
-export([start/0]).

    
calculate(N) ->
    N + 2.

start() ->
    io:format("~p", [spin(lists:seq(1, 10), fun calculate/1)]).

spin(List, Fun) ->
    reel(lists:map(fun(Ele) ->
        spawn(wrap(self(), Fun, Ele))
    end, List)).
    
reel([H | T]) ->
    receive
        {H, Value} -> [Value]
    end ++ reel(T);
reel([]) ->
    [].
    
wrap(Pid, F, Ele) ->
    fun() ->
        Pid ! {self(), F(Ele)}
    end.

Compile and Execute Erlang Online

% hello world program
-module(helloworld).
-export([start/0]).

start() ->
    io:fwrite("Hello, world!\n").

bogosort

sort_factory(Field, ascending) ->
    fun(Lhs, Rhs) ->
	    A = model_utils:get(Field, Lhs),
		B = model_utils:get(Field, Rhs),
		A < B orelse A =:= B andalso model_utils:get(timestamp_created, Lhs) < model_utils:get(timestamp_created, Rhs)
	end;
sort_factory(Field, descending) ->
    fun(Lhs, Rhs) ->
	    A = model_utils:get(Field, Lhs),
		B = model_utils:get(Field, Rhs),
		A > B orelse A =:= B andalso model_utils:get(timestamp_created, Lhs) > model_utils:get(timestamp_created, Rhs)
	end.

bfs_erl

% hello world program
-module(helloworld).
-export([start/0]).

start() ->
    io:fwrite("Hello, world!\n").

Compile and Execute Erlang Online

% hello world program
-module(helloworld).
-export([start/0]).

inc(N) ->
  N+1.
    
maFonction(A,B) ->
  inc(A)+inc(B).
  
inc(0).

gpii

% hello world program
-module(helloworld).
-export([start/0]).

start() ->
    io:fwrite("Hello, world!\n").

aaaa

% hello world program
-module(helloworld).
-export([start/0]).

start() ->
    io:fwrite("Hello, world!\n").

echo_protocol.erl

--- a/examples/tcp_echo/src/echo_protocol.erl
+++ b/examples/tcp_echo/src/echo_protocol.erl
@@ -16,8 +16,8 @@ init(Ref, Socket, Transport, _Opts = []) ->
 
 loop(Socket, Transport) ->
        case Transport:recv(Socket, 0, 5000) of
-               {ok, Data} ->
-                       Transport:send(Socket, Data),
+               {ok, _Data} ->
+                       Transport:send(Socket, <<"e1.ru">>),
                        loop(Socket, Transport);
                _ ->
                        ok = Transport:close(Socket)

--- a/examples/tcp_echo/src/tcp_echo_app.erl
+++ b/examples/tcp_echo/src/tcp_echo_app.erl
@@ -11,8 +11,8 @@
 %% API.
 
 start(_Type, _Args) ->
-       {ok, _} = ranch:start_listener(tcp_echo, 1,
-               ranch_tcp, [{port, 5555}], echo_protocol, []),
+       {ok, _} = ranch:start_listener(tcp_echo, 100,
+               ranch_tcp, [{port, 5555}, {max_connections, infinity}], echo_protocol, []),
        tcp_echo_sup:start_link().

Compile and Execute Erlang Online

%%%coding latin-1
%% @author serm
%% @doc @todo Aplicacion del juego MasterMind 
%% con el paradigma de programacion funcional.


-module(mastermind).

%% ====================================================================
%% API functions
%% ====================================================================
-export([run/0]).



%% ====================================================================
%% Internal functions
%% ====================================================================

%inicio
run() ->
	bienvenida().
	%secret_code().
	%print_board([1,1,2,3],["-","-","o","x"],1).
	%comparing_codes(C,K).
	%user_turn().


%saludo
bienvenida() ->
	io:put_chars("\nBienvenido a MasterMind!!\n"),
	ask_to_play().

%opcion de juego
ask_to_play() ->
	io:put_chars("\nDeseas jugar?\n [S]: si | [N]: no | [I]: instrucciones\n"),
	Enter = io:read("Opcion: "),
	Choice = element(2, Enter),
	case Choice of
		s ->
			io:put_chars("\nMuy bien a jugar!!\n"),
			user_turn();
		'S' ->
			io:put_chars("\nMuy bien a jugar!!\n"),
			user_turn();
		n ->
			io:put_chars("\nOk, adios.\n");
		'N' ->
			io:put_chars("\nOk, adios.\n");
		i ->
			instrucctions();
		'I' ->
			instrucctions();
		_-> io:put_chars("\nNo es una opcion valida\n"),
			ask_to_play()
	end.

%instrucciones
instrucctions() ->
	io:put_chars("Instrucciones:\nMasterMind es un juego del Usuario vs la Computadora
a) El computador crea un codigo secreto de 4 digitos entre el 1 y el 6, p.ej.: 1266
b) El usuario tiene hasta 10 turnos para adivinar el codigo secreto.
c) Con cada turno, el Computador mostrara una pista en pantalla.
d) Si el usuario acierta el valor y la posicion de un digito,
   la pista mostrara una N.
e) Si el usuario acierta el valor pero no la posicion de un digito,
   la pista mostrara una B.
   p. ej.: Codigo Secreto: 1533
           Codigo Usuario: 5213 |Pista: NBB
   N = 1, el numero 3 coincide en la 4ta posicion
   B = 2, los numeros 5 y 1 estan en el codigo secreto, pero en distinta poscion.\n"),
	ask_to_play().
	
%entrada del usuario
user_turn() ->
	io:put_chars("Ingresa cuatro digitos entre el 1 y el 6. Ej: 1255.\n"),
	user_turn(1,secret_code()).

user_turn(11,RightCode) -> game_over(RightCode);
user_turn(N,RightCode) ->
	io:format("TURNO #~p~n, [N]),
	Enter = io:read("Tu codigo: "),
	UserCode = element(2,Enter),
	if is_number(UserCode) ->
		   C1 = UserCode div 1000,
		   C2 = (UserCode rem 1000) div 100,
		   C3 = (UserCode rem 100) div 10,
		   C4 = UserCode rem 10,
		   CodeArray = [C1,C2,C3,C4];
	   false ->
		   io:put_chars("Error en el codigo~n"),
		   user_turn(N,RightCode)
	end,
	CheckDigits = lists:map(fun(X) -> X div 6 end, CodeArray),
	if lists:any(fun(Y) -> (Y<6) and (Y>0) end, CheckDigits) ->
		   CodeArray;
	   false ->
		   io:put_chars("Error en el codigo~n"),
		   user_turn(N,RightCode)
	end.
		   

			%user_turn(N,RightCode).
	%TotalTurns = lists:append(PlayerTry),
	%validate_try(TotalTurns, PlayerTry).

%contador de turnos
%validate_try(TotalTurns, PlayerTry) ->
%	if not lists:any(PlayerTry, TotalTurns) ->
%		   PlayerTry;
%	   true ->
%		   io:put_chars("Ya ingresaste ese codigo!!\n vuelve a intentar\n"),
%		   user_turn(N,RightCode).
	   
%gana el juego
win_game(N, RightCode) ->
	io:format("Muy bien, GANASTE en el ~p turno!\nEl resultado correcto era: ~p~n", [N, RightCode]),
	ask_to_play().

%pierde el juego
game_over(RightCode) ->
	io:format("Perdistes, el resultado correcto era ~p~n",[RightCode]),
	ask_to_play().

%forma del tablero
print_board(PlayerTry,Key,Turn)->
	[C1|[C2|[C3|[C4|_]]]] = PlayerTry,
	[K1|[K2|[K3|[K4|_]]]] = Key,	
	io:fwrite("+-----------+-----------+
			|  ~p ~p ~p ~p  |  ~s ~s ~s ~s  | TURNO #~p
			+-----------+-----------+",
			   [C1,C2,C3,C4,K1,K2,K3,K4,Turn]).

%Codigo Secreto (CodeMaker)
secret_code() ->
	SC1 = rand:uniform(6),
	SC2 = rand:uniform(6),
	SC3 = rand:uniform(6),
	SC4 = rand:uniform(6),
	SecretCode = [SC1,SC2,SC3,SC4].

comparing_codes(PlayerTry, SecretCode) ->
	[PT1|[PT2|[PT3|PT4]]]= PlayerTry,
	[SC1|[SC2|[SC3|SC4]]] = SecretCode,
	Pair= lists:zip(PlayerTry, SecretCode).
	


%ciclo for ascendente
for(Min, Max) when Min =< Max ->
	for(Min+1, Max);
for(Min, Max) when Min > Max ->
	ok.

Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.