code_junkie

New to functional programming

Nov 12th, 2011
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. import Array
  2. import Char
  3.  
  4. trans :: Int -> Int -> Double -> Double -> Double
  5. trans from x trans11 trans21 =
  6. if (from == 1) && (x == 1)
  7. then trans11
  8. else if (from == 1) && (x == 2)
  9. then (1-trans11)
  10. else if (from == 2) && (x == 1)
  11. then trans21
  12. else (1-trans21)
  13.  
  14. em :: Char -> [Double] -> Double
  15. em c list = list!! a
  16. where a = digitToInt c
  17.  
  18. intToChar :: Int -> Char
  19. intToChar n | n == 1 = '1'
  20. | n == 2 = '2'
  21.  
  22. casino :: Char -> Int -> Int -> [Double] -> [Double] -> Double -> Double -> Double
  23. casino seqchar 1 y em1 em2 t1 t2= 0.5 * (em seqchar em1)
  24. casino seqchar 2 y em1 em2 t1 t2= 0.5 * (em seqchar em2)
  25. casino seqchar x y em1 em2 t1 t2= maximum[ (1 @@ y-1)*(em seqchar em1)*(trans 1 x t1 t2),(2 @@ y-1)*(em seqchar em2)*(trans 2 x t1 t2) ]
  26.  
  27. dynamicProgram :: [Char] -> (Char -> Int -> Int -> [Double] -> [Double] -> Double -> Double -> Double) -> [Double] -> [Double] -> Double -> Double -> (Array a b)
  28. dynamicProgram string score list1 list2 trans11 trans21 = myArray 1 len
  29. [score (string!!y) x y list1 list2 trans11 trans21 | x Int -> [Double] -> Array a b
  30. myArray startIndex endIndex values = listArray (startIndex,startIndex) (endIndex,endIndex) values
  31.  
  32. traceback :: [Char] -> Int -> Int -> [Double] -> [Double] -> Double -> Double -> [Char]
  33. traceback s 1 0 em1 em2 t1 t2 = []
  34. traceback s 2 0 em1 em2 t1 t2 = []
  35. traceback s x y em1 em2 t1 t2 | x@@y == (1 @@ y-1)*(em (s!!y) em1)*(trans 1 x t1 t2) = '1' : traceback s 1 (y-1) em1 em2 t1 t2
  36. | x@@y == (2 @@ y-1)*(em (s!!y) em1)*(trans 2 x t1 t2) = '2' : traceback s 2 (y-1) em1 em2 t1 t2
  37.  
  38. answer :: [Char] -> [Double] -> [Double] -> Double -> Double -> [Char]
  39. answer string list1 list2 t1 t2 = reverse $ maxC : traceback string max end list1 list2 t1 t2 $ dynamicProgram casino string list1 list2 t1 t2
  40. where
  41. end = (length string) + 1
  42. max | maximum (1@@end) (2@@end) == 1@@end = 1
  43. | maximum (1@@end) (2@@end) == 2@@end = 2
  44. maxC = intToChar max
  45.  
  46. infix 5 @@
  47. (@@) i j = myArray ! (i, j)
  48.  
  49. main = do
  50. putStrLn "What is the sequence to test?"
  51. seq state 1 transmission probability?"
  52. trp1 state 2 transmission probability is " ++ (1-trp1)
  53. putStrLn "What is the state 2 -> state 1 transmission probability?"
  54. trp2 state 2 transmission probability is " ++ (1-trp2)
  55. putStrLn "I assume that the prob of starting in either state is 1/2. Go!"
  56. answer seq st1 st2 trp1 trp2
  57.  
  58. foo.hs:34:71:
  59. Couldn't match expected type `[e]' against inferred type `(a, b)'
  60. In the second argument of `listArray', namely
  61. `(endIndex, endIndex)'
  62. In the expression:
  63. listArray (startIndex, startIndex) (endIndex, endIndex) values
  64. In the definition of `myArray':
  65. myArray startIndex endIndex values
  66. = listArray (startIndex, startIndex) (endIndex, endIndex) values
  67.  
  68. listArray :: (Ix i) => (i, i) -> [e] -> Array i e
  69. -- Defined in GHC.Arr
  70.  
  71. listArray (startIndex, endIndex) values
  72.  
  73. foo.hs:43:44:
  74. Couldn't match expected type `a -> b'
  75. against inferred type `[Char]'
  76. In the first argument of `($)', namely
  77. `maxC : (traceback string max end list1 list2 t1 t2)'
  78. In the second argument of `($)', namely
  79. `(maxC : (traceback string max end list1 list2 t1 t2))
  80. $ (dynamicProgram casino string list1 list2 t1 t2)'
  81. In the expression:
  82. reverse
  83. $ ((maxC : (traceback string max end list1 list2 t1 t2))
  84. $ (dynamicProgram casino string list1 list2 t1 t2))
  85.  
  86. foo.hs:51:11:
  87. Couldn't match expected type `Array i e'
  88. against inferred type `Int -> Int -> [Double] -> Array a b'
  89. In the first argument of `(!)', namely `myArray'
  90. In the expression: myArray ! (i, j)
  91. In the definition of `@@': @@ i j = myArray ! (i, j)
  92.  
  93. foo.hs:63:4:
  94. Couldn't match expected type `[a]' against inferred type `IO ()'
  95. In the first argument of `(++)', namely
  96. `putStrLn
  97. "I assume that the state 1 -> state 2 transmission probability is "'
  98. In the expression:
  99. (putStrLn
  100. "I assume that the state 1 -> state 2 transmission probability is ")
  101. ++
  102. (1 - trp1)
  103. In a 'do' expression:
  104. (putStrLn
  105. "I assume that the state 1 -> state 2 transmission probability is ")
  106. ++
  107. (1 - trp1)
  108.  
  109. listArray ((startIndex, startIndex), (endIndex, endIndex)) values
  110.  
  111. trans :: Int -> Int -> Double -> Double -> Double
  112. trans 1 1 trans11 trans21 = trans11
  113. trans 1 2 trans11 trans21 = 1-trans11
  114. trans 2 1 trans11 trans21 = trans21
  115. trans _ _ trans11 trans21 = 1-trans21
Add Comment
Please, Sign In to add comment