Guest User

Untitled

a guest
Feb 16th, 2016
69
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Data.Char
  2.  
  3. -- Convert pure text to word lists removing symbols
  4. asWords :: [String] -> [[String]]
  5. asWords = map $ words . map r where r c | isAlpha c = c
  6.                                         | otherwise = ' '
  7.  
  8. -- Lookup last row indexed word
  9. jump :: [[String]] -> Int -> Maybe String
  10. jump []           _ = Nothing
  11. jump [(x:_)]      0 = Just x
  12. jump ((x:xs):xss) 0 = jump (xs:xss) (length x)
  13. jump ([]:xss)     n = jump xss n
  14. jump ((x:xs):xss) n = jump (xs:xss) (n - 1)
  15.  
  16. -- With some word check if obey the law
  17. obey :: String -> [[String]] -> Bool
  18. obey _ [] = False
  19. obey w xs@(x:_) = all (Just w ==) $ (jump xs) <$> [1..length x]
  20.  
  21.  
  22. -- Example
  23. text =
  24.     "In the beginning God created the heaven and the earth.\n\
  25.    \And the earth was without form, and void; and darkness was upon the face of the deep. And the Spirit of God moved upon the face of the waters.\n\
  26.    \And God said, Let there be light: and there was light"
  27.  
  28. main = print $ obey "God" $ asWords $ lines text
RAW Paste Data