Skip to content

Instantly share code, notes, and snippets.

@paucus
Created March 9, 2017 15:38
Show Gist options
  • Save paucus/1a26d297c00d79b1e34b2bb6f7e331ea to your computer and use it in GitHub Desktop.
Save paucus/1a26d297c00d79b1e34b2bb6f7e331ea to your computer and use it in GitHub Desktop.
%%% assignment 2.15 palindromes
-module(a215).
-include_lib("eunit/include/eunit.hrl").
-created_by("Marcelo Ruiz Camauër").
-export([palindrome/1]).
-export([remove_punctuation/2]).
-spec palindrome(list()) -> boolean().
palindrome([])->false;
palindrome(L)->
M=remove_punctuation(L,[]),
N=lists:reverse(M),
N==M.
%% helper functions. Inspired/copied from Hynek Vychodil
remove_punctuation([],Normalized)->
Normalized;
remove_punctuation([H|T],Normalized) when H>= $a, H=< $z ->
remove_punctuation(T,[H|Normalized]);
remove_punctuation([H|T],Normalized) when H>= $A, H=< $Z ->
remove_punctuation(T,[H+32|Normalized]);
remove_punctuation([_|T],Normalized) ->
% io:format("remove_punctuation ~s ~n",[T]),
remove_punctuation(T,Normalized).
%%%%%%%%%%%%%%%
% invoke with 'a215:test().' or "eunit:test(a215)."
palindrome_test_()->
[ ?_assert(palindrome("A man, a plan, a canal: Panama!")==true),
?_assert(palindrome("Able was I ere I saw Elba")==true),
?_assert(palindrome("Madam, I'm Adam")==true),
?_assert(palindrome("Never odd or even")==true),
?_assert(palindrome("Doc, note: I dissent. A fast never prevents a fatness.
I diet on cod")==true),
?_assert(palindrome("T. Eliot, top bard, notes putrid tang emanating, is
sad; I'd assign it a name: gnat dirt upset on drab pot
toilet.")==true),
?_assert(palindrome([])==false),
?_assert(palindrome("A man, no plan, bad canal: Panama")==false)
].
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment