Guest User

Untitled

a guest
Apr 27th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. import Ratio
  2.  
  3. -- función lineal; x --> (a,b) = ax + b
  4.  
  5. type FL = (Ratio Integer, Ratio Integer)
  6.  
  7. -- Composición
  8. -- x --> (a, b) · (c, d) = c(ax + b) + d = (acx) + (bc + d)
  9. -- (a, b) · (c, d) = (ac, bc + d)
  10.  
  11. (·) :: FL -> FL -> FL
  12. (a,b) · (c,d) = (a*c, b*c + d)
  13.  
  14. -- Potencia. FIXME
  15. (··) :: FL -> Integer -> FL
  16. f ·· 1 = f
  17. f ·· n = f · (f ·· (n-1))
  18.  
  19. phi :: FL
  20. ji :: FL
  21.  
  22. -- sube; completo (10)
  23. phi = (3,1)·ji
  24.  
  25. -- sube; unitario (1)
  26. psi = (3,1)
  27.  
  28. -- baja (0)
  29. ji = (1%2,0)
  30.  
  31. -- "True" es "sube", "False" es "baja". Convierte num. binario en FL
  32. convierte :: [Bool] -> FL
  33. convierte [] = (0,0)
  34. convierte xs = foldl1 (·) $ map (\k -> if k then psi else ji) xs
  35.  
  36. -- a binario/FL desde cadena
  37. b :: String -> [FL]
  38. b [] = []
  39. b (x:xs) = siguiente:(b xs) where siguiente = if x == '0' then ji else psi
  40.  
  41. aplica :: FL -> Ratio Integer -> Ratio Integer
  42. aplica (a,b) x = a*x + b
  43.  
  44. r :: String -> Ratio Integer -> Ratio Integer
  45. r xs n = (aplica $ foldl1 (·) $ b xs) n
Add Comment
Please, Sign In to add comment