Last active
April 5, 2017 02:43
-
-
Save calopez/1c375932829d88898a55d51b1e86d072 to your computer and use it in GitHub Desktop.
Revisions
-
Carlos Andres Lopez revised this gist
Apr 5, 2017 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -109,7 +109,7 @@ response(String) -> Be = if Is =:= true -> " is "; true -> " is not " end, {result, lists:flatten([$", String, $", Be, "palindrome"])}. %% ------ -
Carlos Andres Lopez revised this gist
Apr 5, 2017 . 1 changed file with 31 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -70,20 +70,48 @@ client() -> %% Server %% ------- server() -> Workers = palin_workers(), receive {check, Client, Payload} -> lb(Workers, Client, Payload), server(); stop -> io:format("stopped ~n") end. lb(Workers, Client, Payload) when is_pid(Client) -> Worker = element(rand:uniform(2), Workers), Worker ! {Client, Payload}. palin_workers() -> { spawn(fun worker1/0), spawn(fun worker2/0) }. worker1() -> receive {Client, Payload} -> io:format("response from Sever 1!~n"), Client ! response(Payload) end. worker2() -> receive {Client, Payload} -> io:format("response from Sever 2!~n"), Client ! response(Payload) end. response(String) -> Is = palindrome(String), Be = if Is =:= true -> " is "; true -> " is not " end, {result, lists:flatten(["\"", String, "\"", Be, "palindrome"])}. %% ------ %% Usage %% ------ -
Carlos Andres Lopez created this gist
Apr 3, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,94 @@ -module(palin). -export([ palin/1, nopunct/1, palindrome/1, client/0, server/0 ]). % palindrome problem % % palindrome("Madam I\'m Adam.") = true palindrome(Xs) -> palin(nocaps(nopunct(Xs))). nopunct([]) -> []; nopunct([X|Xs]) -> case lists:member(X,".,\ ;:\t\n\'\"") of true -> nopunct(Xs); false -> [ X | nopunct(Xs) ] end. nocaps([]) -> []; nocaps([X|Xs]) -> [ nocap(X) | nocaps(Xs) ]. nocap(X) -> case $A =< X andalso X =< $Z of true -> X+32; false -> X end. % literal palindrome palin(Xs) -> Xs == reverse(Xs). reverse(Xs) -> shunt(Xs,[]). shunt([],Ys) -> Ys; shunt([X|Xs],Ys) -> shunt(Xs,[X|Ys]). %% ------- %% Client %% ------- client() -> receive {send, Pid, Msg} -> Pid ! Msg; {result, Msg } -> io:format("client received: ~p~n", [Msg]), client(); stop -> io:format("client stopped!") end. %% ------- %% Server %% ------- server() -> receive {check, Pid, String} -> Pid ! response(String), server(); stop -> io:format("stopped ~n") end. response(String) -> Is = palindrome(String), Be = if Is =:= true -> " is "; true -> " is not " end, {result, lists:flatten(["\"", String, "\"", Be, "palindrome"])}. %% ------ %% Usage %% ------ %% 1> Server = spawn(palin, server, []). %% 2> Client = spawn(palin, client, []). %% 3> Server ! {check, Client, "Air an aria"}. %% 4> Server ! {check, self(), "Air an aria"}. %% 5> flush().