Created
March 9, 2017 15:38
-
-
Save paucus/1a26d297c00d79b1e34b2bb6f7e331ea to your computer and use it in GitHub Desktop.
This file contains 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 characters
%%% 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