Skip to content

Instantly share code, notes, and snippets.

@jason-carter
Created April 4, 2017 22:06
Show Gist options
  • Save jason-carter/3e0dc20425c44a4bd919990062eb6534 to your computer and use it in GitHub Desktop.
Save jason-carter/3e0dc20425c44a4bd919990062eb6534 to your computer and use it in GitHub Desktop.
FutureLearn Concurrent Programming In Erlang 1.5: Trying it for yourself
-module(palin).
-export([palin/1,nopunct/1,palindrome/1]).
-export([server/1]).
% palindrome server
%
% To Call:
%
% IsPalin = spawn(palin, server, [self()]).
% IsPalin ! {check, "Pop,"}.
%
server(Pid) ->
receive
stop ->
Pid ! io:format("stopped~n");
{check, Msg} ->
case palindrome(Msg) of
true -> Pid ! io:format("{result, ~s is a palindrome}~n", [Msg]);
false -> Pid ! io:format("{result, ~s is NOT a palindrome}~n", [Msg])
end,
server(Pid)
end.
% 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]).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment