Skip to content

Instantly share code, notes, and snippets.

@smarj
Created March 16, 2017 21:56
Show Gist options
  • Save smarj/a8a030f46ea5ad050fef98c633cc055c to your computer and use it in GitHub Desktop.
Save smarj/a8a030f46ea5ad050fef98c633cc055c to your computer and use it in GitHub Desktop.
Functional Programming in Erlang, 2.15
-module(where3).
-export([palindrome/1]).
palindrome(List) ->
real_palindrome(lcase(strip(List))).
real_palindrome([]) ->
true;
real_palindrome([_X|[]]) ->
true;
real_palindrome([F|Xs]) ->
{Rest,[L]} = lists:split(length(Xs) - 1, Xs),
case lowercase(F) =:= lowercase(L) of
true ->
palindrome(Rest);
false ->
false
end.
ctype(C) when C < 65 -> punctuation;
ctype(C) when C > 64 -> letter.
lowercase(C) when C > 65, C < 91 -> C + 32;
lowercase(C) when C > 96, C < 123 -> C;
lowercase(_) -> badarg.
lcase([]) -> [];
lcase([X|Xs]) ->
[lowercase(X)|lcase(Xs)].
strip([]) -> [];
strip([X|Xs]) ->
case ctype(X) of
punctuation ->
strip(Xs);
letter ->
[X|strip(Xs)]
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment