Advertisement
Guest User

Untitled

a guest
Jan 16th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. dec_to_bin :: Int -> [Int]
  2. dec_to_bin x |x > 256 || x < 1 = []
  3.              |x == 0           = take 8 (repeat 0)
  4.              |x == 255         = take 8 (repeat 1)
  5.              |otherwise        = dec_to_bin (div x 2) ++ [mod x 2]
  6.  
  7. bin_to_unicode :: [Int] -> [Char]
  8. bin_to_unicode l |not head l = []
  9.                  |head l == 0 = [' '] ++ bin_to_unicode (tail l)
  10.                  |head l == 1 = ['█'] ++ bin_to_unicode (tail l)
  11.  
  12. -- r rule, i input sequence, n number of iterations
  13. automaton :: Int -> [Int] -> Int -> [[Char]]
  14. automaton r i n |not head i || 255 < r || r < 0 = []
  15.                 |r == 0    = [take (len i) (repeat ' ')] ++ automaton r (take (len i) (repeat ' ')) (n - 1)
  16.                 |r == 255  = [take (len i) (repeat '█')] ++ automaton r (take (len i) (repeat '█')) (n - 1)
  17.                 |otherwise = [aux (dec_to_bin r) i] ++ automaton r (aux r i) (n - 1)
  18.  
  19. aux :: [Int] -> [Int] -> [Char]
  20. aux r i |not head i            = []
  21.         |take 3 i == [1, 1, 1] = [head r] ++ aux r (drop 3 i)
  22.         |take 3 i == [1, 1, 0] = (r !! 1) ++ aux r (drop 3 i)
  23.         |take 3 i == [1, 0, 1] = (r !! 2) ++ aux r (drop 3 i)
  24.         |take 3 i == [1, 0, 0] = (r !! 3) ++ aux r (drop 3 i)
  25.         |take 3 i == [0, 1, 1] = (r !! 4) ++ aux r (drop 3 i)
  26.         |take 3 i == [0, 1, 0] = (r !! 5) ++ aux r (drop 3 i)
  27.         |take 3 i == [0, 0, 1] = (r !! 6) ++ aux r (drop 3 i)
  28.         |take 3 i == [0, 0, 0] = [tail r] ++ aux r (drop 3 i)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement