{-# OPTIONS -fglasgow-exts #-} -- arch-tag: fb034a1c-337c-47c9-93ff-21a7ca096a80 module BubbleBabble(bubbleBabble, bubbleBabbleString) where import Word import Data.Array.Unboxed import Char import Bits import GHC.Exts -- infixr 5 !:, !:! -- x !: xs = seq x (x:xs) -- x !:! xs = seq xs $ seq x $ x:xs vowel :: UArray Int Char vowel = listArray (0,5) "aeiouy" consonant :: UArray Int Char consonant = listArray (0,16) "bcdfghklmnprstvzx" bubbleBabbleString :: String -> String bubbleBabbleString s = bubbleBabble (map (fromIntegral . ord) s) bubbleBabble :: [Word8] -> String bubbleBabble cs = 'x' : bb 1 (map fromIntegral cs) where bb seed [] = vcvx ((seed `mod` 6),16,(seed `div` 6)) bb seed [x] = vcvx ((((x `shiftR` 6) .&. 3) + seed) `mod` 6, (x `shiftR` 2) .&. 15, ((x .&. 3) + (seed `div` 6)) `mod` 6) bb seed (x:y:xs) = vcvcc (a,b,c,d,e) $ bb ((seed * 5 + (x * 7 + y)) `mod` 36) xs where a = (((x `shiftR` 6) .&. 3) + seed) `mod` 6 b = (x `shiftR` 2) .&. 15 c = ((x .&. 3) + (seed `div` 6)) `mod` 6 d = (y `shiftR` 4) .&. 15 e = y .&. 15 vcvx (a,b,c) = vowel!a : consonant!b : vowel!c : "x" vcvcc (a,b,c,d,e) xs = vowel!a : consonant!b : vowel!c : consonant!d : '-' : consonant!e : xs {- --bubbleBabble cs = bubbleBabbleArray (listArray (0, length cs - 1) (map fromIntegral cs)) bubbleBabbleArray :: UArray Int Int -> String bubbleBabbleArray a = 'x' : p where l = snd (bounds a) + 1 l2 = l `div` 2 c :: UArray Int Int c = listArray (1,l2 + 1) (map c' [1 .. l2 + 1]) c' 1 = 1 c' n = (c!(n - 1) * 5 + (a!(n * 2 - 3) * 7 + a!(n * 2 - 2))) `mod` 36 p | l `mod` 2 == 0 = vcvx ((c!(l2 + 1) `mod` 6),16,(c!(l2 + 1) `div` 6)) | otherwise = "x" vcvx (a,b,c) = vowel!a : consonant!b : vowel!c : "x" vcvcc (a,b,c,d,e) xs = vowel!a : consonant!b : vowel!c : consonant!d : '-' : vowel!e : xs -}