As you see, S combinator is quite useful.
Sadly, the type itself is quite long.
- Split the test cases in 2.
module Code (palindrome) where palindrome :: Eq a => [a] -> Bool -- Truly Parametric palindrome = (==) <*> reverse
#include <string>bool isPalindrome(const std::string &word) {return std::string{word.rbegin(),word.rend()}==word;}- module Code (palindrome) where
- palindrome :: Eq a => [a] -> Bool -- Truly Parametric
- palindrome = (==) <*> reverse
module TestSpec (spec) where import Test.Hspec import Code (palindrome) spec :: Spec spec = do it "is palindromes" $ do palindrome "A" `shouldBe` True palindrome "a" `shouldBe` True palindrome "aa" `shouldBe` True palindrome "aba" `shouldBe` True palindrome "acca" `shouldBe` True palindrome "adda" `shouldBe` True palindrome "affa" `shouldBe` True palindrome "aga" `shouldBe` True palindrome "aha" `shouldBe` True palindrome "ajaja" `shouldBe` True palindrome "aka" `shouldBe` True palindrome "ala" `shouldBe` True palindrome "alala" `shouldBe` True palindrome "alula" `shouldBe` True palindrome "ama" `shouldBe` True palindrome "amma" `shouldBe` True palindrome "ana" `shouldBe` True palindrome "anana" `shouldBe` True palindrome "anna" `shouldBe` True palindrome "apa" `shouldBe` True palindrome "ara" `shouldBe` True palindrome "arara" `shouldBe` True palindrome "atta" `shouldBe` True palindrome "ava" `shouldBe` True palindrome "awa" `shouldBe` True palindrome "B" `shouldBe` True palindrome "b" `shouldBe` True palindrome "bib" `shouldBe` True palindrome "bob" `shouldBe` True palindrome "boob" `shouldBe` True palindrome "bub" `shouldBe` True palindrome "C" `shouldBe` True palindrome "c" `shouldBe` True palindrome "civic" `shouldBe` True palindrome "D" `shouldBe` True palindrome "d" `shouldBe` True palindrome "dad" `shouldBe` True palindrome "deed" `shouldBe` True palindrome "deedeed" `shouldBe` True palindrome "degged" `shouldBe` True palindrome "did" `shouldBe` True palindrome "dod" `shouldBe` True palindrome "dud" `shouldBe` True palindrome "E" `shouldBe` True palindrome "e" `shouldBe` True palindrome "eke" `shouldBe` True palindrome "elle" `shouldBe` True palindrome "eme" `shouldBe` True palindrome "ere" `shouldBe` True palindrome "eve" `shouldBe` True palindrome "ewe" `shouldBe` True palindrome "eye" `shouldBe` True palindrome "F" `shouldBe` True palindrome "f" `shouldBe` True palindrome "G" `shouldBe` True palindrome "g" `shouldBe` True palindrome "gag" `shouldBe` True palindrome "gig" `shouldBe` True palindrome "gog" `shouldBe` True palindrome "H" `shouldBe` True palindrome "h" `shouldBe` True palindrome "hah" `shouldBe` True palindrome "hallah" `shouldBe` True palindrome "huh" `shouldBe` True palindrome "I" `shouldBe` True palindrome "i" `shouldBe` True palindrome "ihi" `shouldBe` True palindrome "imi" `shouldBe` True palindrome "immi" `shouldBe` True palindrome "J" `shouldBe` True palindrome "j" `shouldBe` True palindrome "K" `shouldBe` True palindrome "k" `shouldBe` True palindrome "kakkak" `shouldBe` True palindrome "kayak" `shouldBe` True palindrome "keek" `shouldBe` True palindrome "kelek" `shouldBe` True palindrome "L" `shouldBe` True palindrome "l" `shouldBe` True palindrome "lemel" `shouldBe` True palindrome "level" `shouldBe` True palindrome "M" `shouldBe` True palindrome "m" `shouldBe` True palindrome "maam" `shouldBe` True palindrome "madam" `shouldBe` True palindrome "mem" `shouldBe` True palindrome "mesem" `shouldBe` True palindrome "mim" `shouldBe` True palindrome "minim" `shouldBe` True palindrome "mum" `shouldBe` True palindrome "murdrum" `shouldBe` True palindrome "N" `shouldBe` True palindrome "n" `shouldBe` True palindrome "nan" `shouldBe` True palindrome "non" `shouldBe` True palindrome "noon" `shouldBe` True palindrome "nun" `shouldBe` True palindrome "O" `shouldBe` True palindrome "o" `shouldBe` True palindrome "oho" `shouldBe` True palindrome "otto" `shouldBe` True palindrome "P" `shouldBe` True palindrome "p" `shouldBe` True palindrome "pap" `shouldBe` True palindrome "peep" `shouldBe` True palindrome "pep" `shouldBe` True palindrome "pip" `shouldBe` True palindrome "poop" `shouldBe` True palindrome "pop" `shouldBe` True palindrome "pup" `shouldBe` True palindrome "Q" `shouldBe` True palindrome "q" `shouldBe` True palindrome "R" `shouldBe` True palindrome "r" `shouldBe` True palindrome "radar" `shouldBe` True palindrome "redder" `shouldBe` True palindrome "refer" `shouldBe` True palindrome "repaper" `shouldBe` True palindrome "retter" `shouldBe` True palindrome "rever" `shouldBe` True palindrome "reviver" `shouldBe` True palindrome "rotator" `shouldBe` True palindrome "rotor" `shouldBe` True palindrome "S" `shouldBe` True palindrome "s" `shouldBe` True palindrome "siris" `shouldBe` True palindrome "sis" `shouldBe` True palindrome "sooloos" `shouldBe` True palindrome "T" `shouldBe` True palindrome "t" `shouldBe` True palindrome "tat" `shouldBe` True palindrome "tebbet" `shouldBe` True palindrome "teet" `shouldBe` True palindrome "tenet" `shouldBe` True palindrome "terret" `shouldBe` True palindrome "tit" `shouldBe` True palindrome "toot" `shouldBe` True palindrome "tot" `shouldBe` True palindrome "tst" `shouldBe` True palindrome "tut" `shouldBe` True palindrome "tyt" `shouldBe` True palindrome "U" `shouldBe` True palindrome "u" `shouldBe` True palindrome "ulu" `shouldBe` True palindrome "ululu" `shouldBe` True palindrome "umu" `shouldBe` True palindrome "utu" `shouldBe` True palindrome "V" `shouldBe` True palindrome "v" `shouldBe` True palindrome "W" `shouldBe` True palindrome "w" `shouldBe` True palindrome "waw" `shouldBe` True palindrome "wow" `shouldBe` True palindrome "X" `shouldBe` True palindrome "x" `shouldBe` True palindrome "Y" `shouldBe` True palindrome "y" `shouldBe` True palindrome "yaray" `shouldBe` True palindrome "yoy" `shouldBe` True palindrome "Z" `shouldBe` True palindrome "z" `shouldBe` True it "is not palindromes" $ do palindrome "Absence" `shouldBe` False palindrome "Justify" `shouldBe` False palindrome "Academic" `shouldBe` False palindrome "kitchen" `shouldBe` False palindrome "Accompany" `shouldBe` False palindrome "knee" `shouldBe` False palindrome "Accomplish" `shouldBe` False palindrome "knife" `shouldBe` False palindrome "Acknowledge" `shouldBe` False palindrome "knock" `shouldBe` False palindrome "Adapt" `shouldBe` False palindrome "know" `shouldBe` False palindrome "Administration" `shouldBe` False palindrome "Knowledge" `shouldBe` False palindrome "Affect" `shouldBe` False palindrome "land" `shouldBe` False palindrome "Afford " `shouldBe` False palindrome "landscape" `shouldBe` False palindrome "Analysis" `shouldBe` False palindrome "language" `shouldBe` False palindrome "Apparently" `shouldBe` False palindrome "lap" `shouldBe` False palindrome "Background" `shouldBe` False palindrome "large" `shouldBe` False palindrome "Balance" `shouldBe` False palindrome "largely" `shouldBe` False palindrome "Besides" `shouldBe` False palindrome "Last" `shouldBe` False palindrome "Beside" `shouldBe` False palindrome "manufacturing" `shouldBe` False palindrome "Beyond" `shouldBe` False palindrome "many" `shouldBe` False palindrome "Borrow" `shouldBe` False palindrome "map" `shouldBe` False palindrome "Boundary" `shouldBe` False palindrome "margin" `shouldBe` False palindrome "Budget" `shouldBe` False palindrome "mark" `shouldBe` False palindrome "Building" `shouldBe` False palindrome "market" `shouldBe` False palindrome "Business" `shouldBe` False palindrome "Marketing" `shouldBe` False palindrome "Calculate" `shouldBe` False palindrome "negotiation" `shouldBe` False palindrome "Campaign" `shouldBe` False palindrome "neighbor" `shouldBe` False palindrome "Campus" `shouldBe` False palindrome "neighborhood" `shouldBe` False palindrome "Capability" `shouldBe` False palindrome "neither" `shouldBe` False palindrome "Capacity" `shouldBe` False palindrome "Nerve" `shouldBe` False palindrome "Category" `shouldBe` False palindrome "objective" `shouldBe` False palindrome "Challenge" `shouldBe` False palindrome "obligation" `shouldBe` False palindrome "Characteristic" `shouldBe` False palindrome "observation" `shouldBe` False palindrome "Civil" `shouldBe` False palindrome "Observe" `shouldBe` False palindrome "Coalition" `shouldBe` False palindrome "opportunity" `shouldBe` False palindrome "Colonial" `shouldBe` False palindrome "Oppose" `shouldBe` False palindrome "Deal" `shouldBe` False palindrome "porch" `shouldBe` False palindrome "Defensive" `shouldBe` False palindrome "port" `shouldBe` False palindrome "Democracy" `shouldBe` False palindrome "portion" `shouldBe` False palindrome "Description" `shouldBe` False palindrome "portrait" `shouldBe` False palindrome "Dialogue" `shouldBe` False palindrome "portray" `shouldBe` False palindrome "Dimension" `shouldBe` False palindrome "pose" `shouldBe` False palindrome "Discrimination" `shouldBe` False palindrome "Position" `shouldBe` False palindrome "Distinction" `shouldBe` False palindrome "qualify" `shouldBe` False palindrome "Distinguish" `shouldBe` False palindrome "quality" `shouldBe` False palindrome "elementary" `shouldBe` False palindrome "Quarter" `shouldBe` False palindrome "eliminate" `shouldBe` False palindrome "quit" `shouldBe` False palindrome "elite" `shouldBe` False palindrome "quite" `shouldBe` False palindrome "else" `shouldBe` False palindrome "Quote" `shouldBe` False palindrome "elsewhere" `shouldBe` False palindrome "recipe" `shouldBe` False palindrome "e-mail" `shouldBe` False palindrome "Recognition" `shouldBe` False palindrome "embrace" `shouldBe` False palindrome "refugee" `shouldBe` False
//Assert::ThatDescribe(codewarriors) {It(test_basic) {Assert::That(isPalindrome("A"), Equals(true));Assert::That(isPalindrome("a"), Equals(true));Assert::That(isPalindrome("aa"), Equals(true));Assert::That(isPalindrome("aba"), Equals(true));Assert::That(isPalindrome("acca"), Equals(true));Assert::That(isPalindrome("adda"), Equals(true));Assert::That(isPalindrome("affa"), Equals(true));Assert::That(isPalindrome("aga"), Equals(true));Assert::That(isPalindrome("aha"), Equals(true));Assert::That(isPalindrome("ajaja"), Equals(true));Assert::That(isPalindrome("aka"), Equals(true));Assert::That(isPalindrome("ala"), Equals(true));Assert::That(isPalindrome("alala"), Equals(true));Assert::That(isPalindrome("alula"), Equals(true));Assert::That(isPalindrome("ama"), Equals(true));Assert::That(isPalindrome("amma"), Equals(true));Assert::That(isPalindrome("ana"), Equals(true));Assert::That(isPalindrome("anana"), Equals(true));Assert::That(isPalindrome("anna"), Equals(true));Assert::That(isPalindrome("apa"), Equals(true));Assert::That(isPalindrome("ara"), Equals(true));Assert::That(isPalindrome("arara"), Equals(true));Assert::That(isPalindrome("atta"), Equals(true));Assert::That(isPalindrome("ava"), Equals(true));Assert::That(isPalindrome("awa"), Equals(true));Assert::That(isPalindrome("B"), Equals(true));Assert::That(isPalindrome("b"), Equals(true));Assert::That(isPalindrome("bib"), Equals(true));Assert::That(isPalindrome("bob"), Equals(true));Assert::That(isPalindrome("boob"), Equals(true));Assert::That(isPalindrome("bub"), Equals(true));Assert::That(isPalindrome("C"), Equals(true));Assert::That(isPalindrome("c"), Equals(true));Assert::That(isPalindrome("civic"), Equals(true));Assert::That(isPalindrome("D"), Equals(true));Assert::That(isPalindrome("d"), Equals(true));Assert::That(isPalindrome("dad"), Equals(true));Assert::That(isPalindrome("deed"), Equals(true));Assert::That(isPalindrome("deedeed"), Equals(true));Assert::That(isPalindrome("degged"), Equals(true));Assert::That(isPalindrome("did"), Equals(true));Assert::That(isPalindrome("dod"), Equals(true));Assert::That(isPalindrome("dud"), Equals(true));Assert::That(isPalindrome("E"), Equals(true));Assert::That(isPalindrome("e"), Equals(true));Assert::That(isPalindrome("eke"), Equals(true));Assert::That(isPalindrome("elle"), Equals(true));Assert::That(isPalindrome("eme"), Equals(true));Assert::That(isPalindrome("ere"), Equals(true));Assert::That(isPalindrome("eve"), Equals(true));Assert::That(isPalindrome("ewe"), Equals(true));Assert::That(isPalindrome("eye"), Equals(true));Assert::That(isPalindrome("F"), Equals(true));Assert::That(isPalindrome("f"), Equals(true));Assert::That(isPalindrome("G"), Equals(true));Assert::That(isPalindrome("g"), Equals(true));Assert::That(isPalindrome("gag"), Equals(true));Assert::That(isPalindrome("gig"), Equals(true));Assert::That(isPalindrome("gog"), Equals(true));Assert::That(isPalindrome("H"), Equals(true));Assert::That(isPalindrome("h"), Equals(true));Assert::That(isPalindrome("hah"), Equals(true));Assert::That(isPalindrome("hallah"), Equals(true));Assert::That(isPalindrome("huh"), Equals(true));Assert::That(isPalindrome("I"), Equals(true));Assert::That(isPalindrome("i"), Equals(true));Assert::That(isPalindrome("ihi"), Equals(true));Assert::That(isPalindrome("imi"), Equals(true));Assert::That(isPalindrome("immi"), Equals(true));Assert::That(isPalindrome("J"), Equals(true));Assert::That(isPalindrome("j"), Equals(true));Assert::That(isPalindrome("K"), Equals(true));Assert::That(isPalindrome("k"), Equals(true));Assert::That(isPalindrome("kakkak"), Equals(true));Assert::That(isPalindrome("kayak"), Equals(true));Assert::That(isPalindrome("keek"), Equals(true));Assert::That(isPalindrome("kelek"), Equals(true));Assert::That(isPalindrome("L"), Equals(true));Assert::That(isPalindrome("l"), Equals(true));Assert::That(isPalindrome("lemel"), Equals(true));Assert::That(isPalindrome("level"), Equals(true));Assert::That(isPalindrome("M"), Equals(true));Assert::That(isPalindrome("m"), Equals(true));Assert::That(isPalindrome("maam"), Equals(true));Assert::That(isPalindrome("madam"), Equals(true));Assert::That(isPalindrome("mem"), Equals(true));Assert::That(isPalindrome("mesem"), Equals(true));Assert::That(isPalindrome("mim"), Equals(true));Assert::That(isPalindrome("minim"), Equals(true));Assert::That(isPalindrome("mum"), Equals(true));Assert::That(isPalindrome("murdrum"), Equals(true));Assert::That(isPalindrome("N"), Equals(true));Assert::That(isPalindrome("n"), Equals(true));Assert::That(isPalindrome("nan"), Equals(true));Assert::That(isPalindrome("non"), Equals(true));Assert::That(isPalindrome("noon"), Equals(true));Assert::That(isPalindrome("nun"), Equals(true));Assert::That(isPalindrome("O"), Equals(true));Assert::That(isPalindrome("o"), Equals(true));Assert::That(isPalindrome("oho"), Equals(true));Assert::That(isPalindrome("otto"), Equals(true));Assert::That(isPalindrome("P"), Equals(true));Assert::That(isPalindrome("p"), Equals(true));Assert::That(isPalindrome("pap"), Equals(true));Assert::That(isPalindrome("peep"), Equals(true));Assert::That(isPalindrome("pep"), Equals(true));Assert::That(isPalindrome("pip"), Equals(true));Assert::That(isPalindrome("poop"), Equals(true));Assert::That(isPalindrome("pop"), Equals(true));Assert::That(isPalindrome("pup"), Equals(true));Assert::That(isPalindrome("Q"), Equals(true));Assert::That(isPalindrome("q"), Equals(true));Assert::That(isPalindrome("R"), Equals(true));Assert::That(isPalindrome("r"), Equals(true));Assert::That(isPalindrome("radar"), Equals(true));Assert::That(isPalindrome("redder"), Equals(true));Assert::That(isPalindrome("refer"), Equals(true));Assert::That(isPalindrome("repaper"), Equals(true));Assert::That(isPalindrome("retter"), Equals(true));Assert::That(isPalindrome("rever"), Equals(true));Assert::That(isPalindrome("reviver"), Equals(true));Assert::That(isPalindrome("rotator"), Equals(true));Assert::That(isPalindrome("rotor"), Equals(true));Assert::That(isPalindrome("S"), Equals(true));Assert::That(isPalindrome("s"), Equals(true));Assert::That(isPalindrome("siris"), Equals(true));Assert::That(isPalindrome("sis"), Equals(true));Assert::That(isPalindrome("sooloos"), Equals(true));Assert::That(isPalindrome("T"), Equals(true));Assert::That(isPalindrome("t"), Equals(true));Assert::That(isPalindrome("tat"), Equals(true));Assert::That(isPalindrome("tebbet"), Equals(true));Assert::That(isPalindrome("teet"), Equals(true));Assert::That(isPalindrome("tenet"), Equals(true));Assert::That(isPalindrome("terret"), Equals(true));Assert::That(isPalindrome("tit"), Equals(true));Assert::That(isPalindrome("toot"), Equals(true));Assert::That(isPalindrome("tot"), Equals(true));Assert::That(isPalindrome("tst"), Equals(true));Assert::That(isPalindrome("tut"), Equals(true));Assert::That(isPalindrome("tyt"), Equals(true));Assert::That(isPalindrome("U"), Equals(true));Assert::That(isPalindrome("u"), Equals(true));Assert::That(isPalindrome("ulu"), Equals(true));Assert::That(isPalindrome("ululu"), Equals(true));Assert::That(isPalindrome("umu"), Equals(true));Assert::That(isPalindrome("utu"), Equals(true));Assert::That(isPalindrome("V"), Equals(true));Assert::That(isPalindrome("v"), Equals(true));Assert::That(isPalindrome("W"), Equals(true));Assert::That(isPalindrome("w"), Equals(true));Assert::That(isPalindrome("waw"), Equals(true));Assert::That(isPalindrome("wow"), Equals(true));Assert::That(isPalindrome("X"), Equals(true));Assert::That(isPalindrome("x"), Equals(true));Assert::That(isPalindrome("Y"), Equals(true));Assert::That(isPalindrome("y"), Equals(true));Assert::That(isPalindrome("yaray"), Equals(true));Assert::That(isPalindrome("yoy"), Equals(true));Assert::That(isPalindrome("Z"), Equals(true));Assert::That(isPalindrome("z"), Equals(true));Assert::That(isPalindrome("Absence"), Equals(false));Assert::That(isPalindrome("Justify"), Equals(false));Assert::That(isPalindrome("Academic"), Equals(false));Assert::That(isPalindrome("kitchen"), Equals(false));Assert::That(isPalindrome("Accompany"), Equals(false));Assert::That(isPalindrome("knee"), Equals(false));Assert::That(isPalindrome("Accomplish"), Equals(false));Assert::That(isPalindrome("knife"), Equals(false));Assert::That(isPalindrome("Acknowledge"), Equals(false));Assert::That(isPalindrome("knock"), Equals(false));Assert::That(isPalindrome("Adapt"), Equals(false));Assert::That(isPalindrome("know"), Equals(false));Assert::That(isPalindrome("Administration"), Equals(false));Assert::That(isPalindrome("Knowledge"), Equals(false));Assert::That(isPalindrome("Affect"), Equals(false));Assert::That(isPalindrome("land"), Equals(false));Assert::That(isPalindrome("Afford "), Equals(false));Assert::That(isPalindrome("landscape"), Equals(false));Assert::That(isPalindrome("Analysis"), Equals(false));Assert::That(isPalindrome("language"), Equals(false));Assert::That(isPalindrome("Apparently"), Equals(false));Assert::That(isPalindrome("lap"), Equals(false));Assert::That(isPalindrome("Background"), Equals(false));Assert::That(isPalindrome("large"), Equals(false));Assert::That(isPalindrome("Balance"), Equals(false));Assert::That(isPalindrome("largely"), Equals(false));Assert::That(isPalindrome("Besides"), Equals(false));Assert::That(isPalindrome("Last"), Equals(false));Assert::That(isPalindrome("Beside"), Equals(false));Assert::That(isPalindrome("manufacturing"), Equals(false));Assert::That(isPalindrome("Beyond"), Equals(false));Assert::That(isPalindrome("many"), Equals(false));Assert::That(isPalindrome("Borrow"), Equals(false));Assert::That(isPalindrome("map"), Equals(false));Assert::That(isPalindrome("Boundary"), Equals(false));Assert::That(isPalindrome("margin"), Equals(false));Assert::That(isPalindrome("Budget"), Equals(false));Assert::That(isPalindrome("mark"), Equals(false));Assert::That(isPalindrome("Building"), Equals(false));Assert::That(isPalindrome("market"), Equals(false));Assert::That(isPalindrome("Business"), Equals(false));Assert::That(isPalindrome("Marketing"), Equals(false));Assert::That(isPalindrome("Calculate"), Equals(false));Assert::That(isPalindrome("negotiation"), Equals(false));Assert::That(isPalindrome("Campaign"), Equals(false));Assert::That(isPalindrome("neighbor"), Equals(false));Assert::That(isPalindrome("Campus"), Equals(false));Assert::That(isPalindrome("neighborhood"), Equals(false));Assert::That(isPalindrome("Capability"), Equals(false));Assert::That(isPalindrome("neither"), Equals(false));Assert::That(isPalindrome("Capacity"), Equals(false));Assert::That(isPalindrome("Nerve"), Equals(false));Assert::That(isPalindrome("Category"), Equals(false));Assert::That(isPalindrome("objective"), Equals(false));Assert::That(isPalindrome("Challenge"), Equals(false));Assert::That(isPalindrome("obligation"), Equals(false));Assert::That(isPalindrome("Characteristic"), Equals(false));Assert::That(isPalindrome("observation"), Equals(false));Assert::That(isPalindrome("Civil"), Equals(false));Assert::That(isPalindrome("Observe"), Equals(false));Assert::That(isPalindrome("Coalition"), Equals(false));Assert::That(isPalindrome("opportunity"), Equals(false));Assert::That(isPalindrome("Colonial"), Equals(false));Assert::That(isPalindrome("Oppose"), Equals(false));Assert::That(isPalindrome("Deal"), Equals(false));Assert::That(isPalindrome("porch"), Equals(false));Assert::That(isPalindrome("Defensive"), Equals(false));Assert::That(isPalindrome("port"), Equals(false));Assert::That(isPalindrome("Democracy"), Equals(false));Assert::That(isPalindrome("portion"), Equals(false));Assert::That(isPalindrome("Description"), Equals(false));Assert::That(isPalindrome("portrait"), Equals(false));Assert::That(isPalindrome("Dialogue"), Equals(false));Assert::That(isPalindrome("portray"), Equals(false));Assert::That(isPalindrome("Dimension"), Equals(false));Assert::That(isPalindrome("pose"), Equals(false));Assert::That(isPalindrome("Discrimination"), Equals(false));Assert::That(isPalindrome("Position"), Equals(false));Assert::That(isPalindrome("Distinction"), Equals(false));Assert::That(isPalindrome("qualify"), Equals(false));Assert::That(isPalindrome("Distinguish"), Equals(false));Assert::That(isPalindrome("quality"), Equals(false));Assert::That(isPalindrome("elementary"), Equals(false));Assert::That(isPalindrome("Quarter"), Equals(false));Assert::That(isPalindrome("eliminate"), Equals(false));Assert::That(isPalindrome("quit"), Equals(false));Assert::That(isPalindrome("elite"), Equals(false));Assert::That(isPalindrome("quite"), Equals(false));Assert::That(isPalindrome("else"), Equals(false));Assert::That(isPalindrome("Quote"), Equals(false));Assert::That(isPalindrome("elsewhere"), Equals(false));Assert::That(isPalindrome("recipe"), Equals(false));Assert::That(isPalindrome("e-mail"), Equals(false));Assert::That(isPalindrome("Recognition"), Equals(false));Assert::That(isPalindrome("embrace"), Equals(false));Assert::That(isPalindrome("refugee"), Equals(false));}};- module TestSpec (spec) where
- import Test.Hspec
- import Code (palindrome)
- spec :: Spec
- spec = do
- it "is palindromes" $ do
- palindrome "A" `shouldBe` True
- palindrome "a" `shouldBe` True
- palindrome "aa" `shouldBe` True
- palindrome "aba" `shouldBe` True
- palindrome "acca" `shouldBe` True
- palindrome "adda" `shouldBe` True
- palindrome "affa" `shouldBe` True
- palindrome "aga" `shouldBe` True
- palindrome "aha" `shouldBe` True
- palindrome "ajaja" `shouldBe` True
- palindrome "aka" `shouldBe` True
- palindrome "ala" `shouldBe` True
- palindrome "alala" `shouldBe` True
- palindrome "alula" `shouldBe` True
- palindrome "ama" `shouldBe` True
- palindrome "amma" `shouldBe` True
- palindrome "ana" `shouldBe` True
- palindrome "anana" `shouldBe` True
- palindrome "anna" `shouldBe` True
- palindrome "apa" `shouldBe` True
- palindrome "ara" `shouldBe` True
- palindrome "arara" `shouldBe` True
- palindrome "atta" `shouldBe` True
- palindrome "ava" `shouldBe` True
- palindrome "awa" `shouldBe` True
- palindrome "B" `shouldBe` True
- palindrome "b" `shouldBe` True
- palindrome "bib" `shouldBe` True
- palindrome "bob" `shouldBe` True
- palindrome "boob" `shouldBe` True
- palindrome "bub" `shouldBe` True
- palindrome "C" `shouldBe` True
- palindrome "c" `shouldBe` True
- palindrome "civic" `shouldBe` True
- palindrome "D" `shouldBe` True
- palindrome "d" `shouldBe` True
- palindrome "dad" `shouldBe` True
- palindrome "deed" `shouldBe` True
- palindrome "deedeed" `shouldBe` True
- palindrome "degged" `shouldBe` True
- palindrome "did" `shouldBe` True
- palindrome "dod" `shouldBe` True
- palindrome "dud" `shouldBe` True
- palindrome "E" `shouldBe` True
- palindrome "e" `shouldBe` True
- palindrome "eke" `shouldBe` True
- palindrome "elle" `shouldBe` True
- palindrome "eme" `shouldBe` True
- palindrome "ere" `shouldBe` True
- palindrome "eve" `shouldBe` True
- palindrome "ewe" `shouldBe` True
- palindrome "eye" `shouldBe` True
- palindrome "F" `shouldBe` True
- palindrome "f" `shouldBe` True
- palindrome "G" `shouldBe` True
- palindrome "g" `shouldBe` True
- palindrome "gag" `shouldBe` True
- palindrome "gig" `shouldBe` True
- palindrome "gog" `shouldBe` True
- palindrome "H" `shouldBe` True
- palindrome "h" `shouldBe` True
- palindrome "hah" `shouldBe` True
- palindrome "hallah" `shouldBe` True
- palindrome "huh" `shouldBe` True
- palindrome "I" `shouldBe` True
- palindrome "i" `shouldBe` True
- palindrome "ihi" `shouldBe` True
- palindrome "imi" `shouldBe` True
- palindrome "immi" `shouldBe` True
- palindrome "J" `shouldBe` True
- palindrome "j" `shouldBe` True
- palindrome "K" `shouldBe` True
- palindrome "k" `shouldBe` True
- palindrome "kakkak" `shouldBe` True
- palindrome "kayak" `shouldBe` True
- palindrome "keek" `shouldBe` True
- palindrome "kelek" `shouldBe` True
- palindrome "L" `shouldBe` True
- palindrome "l" `shouldBe` True
- palindrome "lemel" `shouldBe` True
- palindrome "level" `shouldBe` True
- palindrome "M" `shouldBe` True
- palindrome "m" `shouldBe` True
- palindrome "maam" `shouldBe` True
- palindrome "madam" `shouldBe` True
- palindrome "mem" `shouldBe` True
- palindrome "mesem" `shouldBe` True
- palindrome "mim" `shouldBe` True
- palindrome "minim" `shouldBe` True
- palindrome "mum" `shouldBe` True
- palindrome "murdrum" `shouldBe` True
- palindrome "N" `shouldBe` True
- palindrome "n" `shouldBe` True
- palindrome "nan" `shouldBe` True
- palindrome "non" `shouldBe` True
- palindrome "noon" `shouldBe` True
- palindrome "nun" `shouldBe` True
- palindrome "O" `shouldBe` True
- palindrome "o" `shouldBe` True
- palindrome "oho" `shouldBe` True
- palindrome "otto" `shouldBe` True
- palindrome "P" `shouldBe` True
- palindrome "p" `shouldBe` True
- palindrome "pap" `shouldBe` True
- palindrome "peep" `shouldBe` True
- palindrome "pep" `shouldBe` True
- palindrome "pip" `shouldBe` True
- palindrome "poop" `shouldBe` True
- palindrome "pop" `shouldBe` True
- palindrome "pup" `shouldBe` True
- palindrome "Q" `shouldBe` True
- palindrome "q" `shouldBe` True
- palindrome "R" `shouldBe` True
- palindrome "r" `shouldBe` True
- palindrome "radar" `shouldBe` True
- palindrome "redder" `shouldBe` True
- palindrome "refer" `shouldBe` True
- palindrome "repaper" `shouldBe` True
- palindrome "retter" `shouldBe` True
- palindrome "rever" `shouldBe` True
- palindrome "reviver" `shouldBe` True
- palindrome "rotator" `shouldBe` True
- palindrome "rotor" `shouldBe` True
- palindrome "S" `shouldBe` True
- palindrome "s" `shouldBe` True
- palindrome "siris" `shouldBe` True
- palindrome "sis" `shouldBe` True
- palindrome "sooloos" `shouldBe` True
- palindrome "T" `shouldBe` True
- palindrome "t" `shouldBe` True
- palindrome "tat" `shouldBe` True
- palindrome "tebbet" `shouldBe` True
- palindrome "teet" `shouldBe` True
- palindrome "tenet" `shouldBe` True
- palindrome "terret" `shouldBe` True
- palindrome "tit" `shouldBe` True
- palindrome "toot" `shouldBe` True
- palindrome "tot" `shouldBe` True
- palindrome "tst" `shouldBe` True
- palindrome "tut" `shouldBe` True
- palindrome "tyt" `shouldBe` True
- palindrome "U" `shouldBe` True
- palindrome "u" `shouldBe` True
- palindrome "ulu" `shouldBe` True
- palindrome "ululu" `shouldBe` True
- palindrome "umu" `shouldBe` True
- palindrome "utu" `shouldBe` True
- palindrome "V" `shouldBe` True
- palindrome "v" `shouldBe` True
- palindrome "W" `shouldBe` True
- palindrome "w" `shouldBe` True
- palindrome "waw" `shouldBe` True
- palindrome "wow" `shouldBe` True
- palindrome "X" `shouldBe` True
- palindrome "x" `shouldBe` True
- palindrome "Y" `shouldBe` True
- palindrome "y" `shouldBe` True
- palindrome "yaray" `shouldBe` True
- palindrome "yoy" `shouldBe` True
- palindrome "Z" `shouldBe` True
- palindrome "z" `shouldBe` True
- it "is not palindromes" $ do
- palindrome "Absence" `shouldBe` False
- palindrome "Justify" `shouldBe` False
- palindrome "Academic" `shouldBe` False
- palindrome "kitchen" `shouldBe` False
- palindrome "Accompany" `shouldBe` False
- palindrome "knee" `shouldBe` False
- palindrome "Accomplish" `shouldBe` False
- palindrome "knife" `shouldBe` False
- palindrome "Acknowledge" `shouldBe` False
- palindrome "knock" `shouldBe` False
- palindrome "Adapt" `shouldBe` False
- palindrome "know" `shouldBe` False
- palindrome "Administration" `shouldBe` False
- palindrome "Knowledge" `shouldBe` False
- palindrome "Affect" `shouldBe` False
- palindrome "land" `shouldBe` False
- palindrome "Afford " `shouldBe` False
- palindrome "landscape" `shouldBe` False
- palindrome "Analysis" `shouldBe` False
- palindrome "language" `shouldBe` False
- palindrome "Apparently" `shouldBe` False
- palindrome "lap" `shouldBe` False
- palindrome "Background" `shouldBe` False
- palindrome "large" `shouldBe` False
- palindrome "Balance" `shouldBe` False
- palindrome "largely" `shouldBe` False
- palindrome "Besides" `shouldBe` False
- palindrome "Last" `shouldBe` False
- palindrome "Beside" `shouldBe` False
- palindrome "manufacturing" `shouldBe` False
- palindrome "Beyond" `shouldBe` False
- palindrome "many" `shouldBe` False
- palindrome "Borrow" `shouldBe` False
- palindrome "map" `shouldBe` False
- palindrome "Boundary" `shouldBe` False
- palindrome "margin" `shouldBe` False
- palindrome "Budget" `shouldBe` False
- palindrome "mark" `shouldBe` False
- palindrome "Building" `shouldBe` False
- palindrome "market" `shouldBe` False
- palindrome "Business" `shouldBe` False
- palindrome "Marketing" `shouldBe` False
- palindrome "Calculate" `shouldBe` False
- palindrome "negotiation" `shouldBe` False
- palindrome "Campaign" `shouldBe` False
- palindrome "neighbor" `shouldBe` False
- palindrome "Campus" `shouldBe` False
- palindrome "neighborhood" `shouldBe` False
- palindrome "Capability" `shouldBe` False
- palindrome "neither" `shouldBe` False
- palindrome "Capacity" `shouldBe` False
- palindrome "Nerve" `shouldBe` False
- palindrome "Category" `shouldBe` False
- palindrome "objective" `shouldBe` False
- palindrome "Challenge" `shouldBe` False
- palindrome "obligation" `shouldBe` False
- palindrome "Characteristic" `shouldBe` False
- palindrome "observation" `shouldBe` False
- palindrome "Civil" `shouldBe` False
- palindrome "Observe" `shouldBe` False
- palindrome "Coalition" `shouldBe` False
- palindrome "opportunity" `shouldBe` False
- palindrome "Colonial" `shouldBe` False
- palindrome "Oppose" `shouldBe` False
- palindrome "Deal" `shouldBe` False
- palindrome "porch" `shouldBe` False
- palindrome "Defensive" `shouldBe` False
- palindrome "port" `shouldBe` False
- palindrome "Democracy" `shouldBe` False
- palindrome "portion" `shouldBe` False
- palindrome "Description" `shouldBe` False
- palindrome "portrait" `shouldBe` False
- palindrome "Dialogue" `shouldBe` False
- palindrome "portray" `shouldBe` False
- palindrome "Dimension" `shouldBe` False
- palindrome "pose" `shouldBe` False
- palindrome "Discrimination" `shouldBe` False
- palindrome "Position" `shouldBe` False
- palindrome "Distinction" `shouldBe` False
- palindrome "qualify" `shouldBe` False
- palindrome "Distinguish" `shouldBe` False
- palindrome "quality" `shouldBe` False
- palindrome "elementary" `shouldBe` False
- palindrome "Quarter" `shouldBe` False
- palindrome "eliminate" `shouldBe` False
- palindrome "quit" `shouldBe` False
- palindrome "elite" `shouldBe` False
- palindrome "quite" `shouldBe` False
- palindrome "else" `shouldBe` False
- palindrome "Quote" `shouldBe` False
- palindrome "elsewhere" `shouldBe` False
- palindrome "recipe" `shouldBe` False
- palindrome "e-mail" `shouldBe` False
- palindrome "Recognition" `shouldBe` False
- palindrome "embrace" `shouldBe` False
- palindrome "refugee" `shouldBe` False
This should be O(logn)
algorithm modulo multiplication of biginteger multiplication.
Wish biginteger multiplication was O(n)
for n digits, then this would be O(n log n)
...
EDIT: Tested on ghci, it takes more time to print fib 1000000
than to compute it.
module Example where import Data.Semigroup (stimesMonoid) -- | Fib deciding pair data FibPair = FibPair !Integer !Integer instance Semigroup FibPair where -- Combines m-th and n-th to (m+n)-th FibPair fm_1 fm <> FibPair fn_1 fn = FibPair (fm * fn + fm_1 * fn_1) (fm * (fn + fn_1) + fm_1 * fn) instance Monoid FibPair where mempty = FibPair 1 0 -- (-1)-th and 0-th fib :: Integer -> Integer fib = (\(FibPair _ n) -> n) . (`stimesMonoid` FibPair 0 1)
- module Example where
import Data.Semigroup (Endo(..),stimes)- import Data.Semigroup (stimesMonoid)
- -- | Fib deciding pair
- data FibPair = FibPair !Integer !Integer
- instance Semigroup FibPair where -- Combines m-th and n-th to (m+n)-th
- FibPair fm_1 fm <> FibPair fn_1 fn =
- FibPair (fm * fn + fm_1 * fn_1) (fm * (fn + fn_1) + fm_1 * fn)
- instance Monoid FibPair where
- mempty = FibPair 1 0 -- (-1)-th and 0-th
- fib :: Integer -> Integer
fib = fst . (`appEndo` (0,1)) . (`stimes` Endo ( \ (a,b) -> (b,a+b) ))- fib = (\(FibPair _ n) -> n) . (`stimesMonoid` FibPair 0 1)
- Added 3 more test cases
- Follwing problem description:
Counting from 1 to n, then back to 1 - Wait, why am I doing this?
module NumberPrint where numberprint :: Int -> Integer numberprint n = read . concat $ show <$> [1 .. pred n] <> [n, pred n .. 1]
- module NumberPrint where
- numberprint :: Int -> Integer
numberprint = read . (>>= id) . ((++) <*> (reverse . init)) . (\x -> show <$> [1..x])- numberprint n = read . concat $ show <$> [1 .. pred n] <> [n, pred n .. 1]
module NumberPrintSpec where import Test.Hspec import NumberPrint spec :: Spec spec = do describe "Tests" $ do it "Fixed Tests" $ do numberprint 1 `shouldBe` 1 numberprint 2 `shouldBe` 121 numberprint 10 `shouldBe` 12345678910987654321 numberprint 12 `shouldBe` 1234567891011121110987654321 numberprint 15 `shouldBe` 1234567891011121314151413121110987654321 main = hspec spec
- module NumberPrintSpec where
- import Test.Hspec
- import NumberPrint
- spec :: Spec
- spec = do
- describe "Tests" $ do
- it "Fixed Tests" $ do
- numberprint 1 `shouldBe` 1
- numberprint 2 `shouldBe` 121
- numberprint 10 `shouldBe` 12345678910987654321
- numberprint 12 `shouldBe` 1234567891011121110987654321
- numberprint 15 `shouldBe` 1234567891011121314151413121110987654321
- main = hspec spec
Combinators are useful.
I'd like a safe tail
function though.
EDIT: drop
could be used instead.
<*>
is S
combinator.
- For
A <*> B
, parameter is piped to both A and B - and then, they are applied
- i.e.
A <*> B = \x -> (A x) (B x)
Pattern matching on number is lame, so implemented using a list.
- Pattern
- on each step, 2 is multipled on each element
- 1 is added in between
-
(iterate step initial !!)
could be used to get result of application n times - List monad is great!
- View pattern is unnecessary now.
module Code (powerfulArray) where import Data.List powerfulArray :: Int -> [Int] powerfulArray = (iterate next [] !!) where next l = 1 : (do n <- l; [2 * n, 1])
{-# Language ViewPatterns #-}- module Code (powerfulArray) where
- import Data.List
- powerfulArray :: Int -> [Int]
powerfulArray 0 = []powerfulArray (pred -> n) = arrayN ++ 2^n : arrayNwhere arrayN = powerfulArray n- powerfulArray = (iterate next [] !!) where next l = 1 : (do n <- l; [2 * n, 1])
module TestSpec (spec) where import Code (powerfulArray) import Test.Hspec spec :: Spec spec = do it "is initial cases" $ do powerfulArray 0 `shouldBe` [] powerfulArray 1 `shouldBe` [1] powerfulArray 2 `shouldBe` [1, 2, 1] powerfulArray 3 `shouldBe` [1, 2, 1, 4, 1, 2, 1] powerfulArray 4 `shouldBe` [1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1] powerfulArray 5 `shouldBe` [1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, 16, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1]
- module TestSpec (spec) where
- import Code (powerfulArray)
- import Test.Hspec
- spec :: Spec
- spec = do
it "should test for something" $ do- it "is initial cases" $ do
- powerfulArray 0 `shouldBe` []
- powerfulArray 1 `shouldBe` [1]
- powerfulArray 2 `shouldBe` [1, 2, 1]
- powerfulArray 3 `shouldBe` [1, 2, 1, 4, 1, 2, 1]
- powerfulArray 4 `shouldBe` [1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1]
- powerfulArray 5 `shouldBe` [1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1, 16, 1, 2, 1, 4, 1, 2, 1, 8, 1, 2, 1, 4, 1, 2, 1]