haskellcpu-wordchain

How can I start to solve a word chain in haskell?


Let us know if the word chain game was played correctly! The object of the game is to form another word with the last letter of the previous word. The words are separated by a space. The words function can help you solve this problem.

Each of the following test cases must give a True:

validGame "apple table lo shoulder dog" == True
validGame "converse veg ball tablecloth zsifaf fules" == True
validGame "forest divide west tabortuz" == False
validGame "sobbing guzsalyas sararany nyul leng" == False

Solution

  • validGame :: String -> Bool
    validGame (x:' ':z:xs) = (x==z) && validGame (z:xs)
    validGame (x:xs) = validGame xs
    validGame [] = True