Last active
June 28, 2017 19:21
-
-
Save andreburgaud/d6ad36c00235a101d87f5b6a246cc83a to your computer and use it in GitHub Desktop.
FutureLearn - Functional Programming in Erlang 2.15 - Palindrome
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 characters
-module(pal). | |
-export([test/0,alpha_nums/1,palindrome/1]). | |
%% alph_nums filter outs punctuation or non printable characters. | |
alpha_nums([]) -> []; | |
alpha_nums([X|Xs]) when X >= $A andalso X =< $z -> | |
[X|alpha_nums(Xs)]; | |
alpha_nums([X|Xs]) when X >= $0 andalso X =< $9 -> | |
[X|alpha_nums(Xs)]; | |
alpha_nums([_|Xs]) -> | |
alpha_nums(Xs). | |
%% palindrome makes use or erlang modules (lists and string) | |
-spec palindrome([_]) -> boolean(). | |
palindrome([]) -> true; | |
palindrome(Xs) -> | |
A = string:to_lower(alpha_nums(Xs)), | |
A == lists:reverse(A). | |
test() -> | |
true = palindrome("Madam I\'m Adam"), | |
true = palindrome("12321"), | |
true = palindrome(""), | |
true = palindrome([]), | |
success. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment