Want more features on Pastebin? Sign Up, it's FREE!
Guest

FP

By: Mellowlicious on Sep 21st, 2011  |  syntax: Haskell  |  size: 0.88 KB  |  views: 150  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. module ThueMorse where
  2. tms1 :: [Int]
  3. tms1 = buildtms1 [0] 1
  4.         where buildtms1 x n    
  5.                 |(n `rem` 2 == 0) = buildtms1 (x++[(x !! (n `div` 2))]) (n+1)
  6.                 |(n `rem` 2 == 1) = buildtms1 (x++[1- (x !! ((n-1) `div` 2))]) (n+1)
  7.                
  8. custinv []      = []
  9. custinv x       = (1-head x):(custinv (tail x))
  10.  
  11. tms3 :: [Int]
  12. tms3 = buildtms3 [0] 1
  13.         where buildtms3 x n = buildtms3 (x++(custinv x)) (n*2)
  14.                
  15. intToBinary :: Int -> [Bool]
  16. intToBinary n   | (n==0) = []
  17.                                 | (n `rem` 2 ==0) = intToBinary (n `div` 2) ++ [False]
  18.                                 | (n `rem` 2 ==1) = intToBinary (n `div` 2) ++ [True]
  19.                                
  20. amountTrue :: [Bool] -> Int
  21. amountTrue [] = 0
  22. amountTrue (x:xs)       | (x==True) = 1+amountTrue(xs)
  23.                                         | (x==False) = amountTrue(xs)
  24.  
  25. tms4 :: [Int]
  26. tms4= buildtms4 0
  27.         where buildtms4 n
  28.                 |(amountTrue (intToBinary n) `rem` 2 ==0) = 0:(buildtms4 (n+1))
  29.                 |(amountTrue (intToBinary n) `rem` 2 ==1) = 1:(buildtms4 (n+1))
clone this paste RAW Paste Data