我对 Haskell 中的数据类型有一点问题,我想我应该先发布一些代码来帮助理解问题
helper :: (MonadMask a, MonadIO a, Functor a) => Expr -> String -> a (Either InterpreterError Int)
helper x y = ( getEval ( mkCodeString x y ) )
-- Creates Code String
mkCodeString :: (Show a) => a -> String -> String
mkCodeString x y = unpack (replace (pack "Const ") (pack "") (replace (pack "\"") (pack "") (replace (pack "Add") (pack y) (pack (show x) ) ) ) )
-- Calculates String
getEval :: (MonadMask m, MonadIO m, Functor m) => [Char] -> m (Either InterpreterError Int)
getEval str = (runInterpreter (setImports ["Prelude"] >> interpret str (as ::Int)))
-- | A test expression.
testexpression1 :: Expr
testexpression1 = 3 + (4 + 5)
-- | A test expression.
testexpression2 :: Expr
testexpression2 = (3 + 4) + 5
-- | A test expression.
testexpression3 :: Expr
testexpression3 = 2 + 5 + 5
我使用像“helper testexpression3”(+)这样的辅助函数,它返回值“Right 12”,类型为“Either InterpreterError Int”,但我只想拥有“Int”值“12”
我尝试了函数->“getValue(Right x)= x”,但我没有得到那个 Int 值。经过一段时间的测试,我认为这是我使用的 Monads 的问题。
如果我像这样测试辅助函数的类型: ":t (helper testexpression1 "(+)")" 我会得到: "(... :: (Functor a, MonadIO a, MonadMask a) => a (InterpreterError Int)"
我怎样才能使这样的事情起作用:写“getValue(helper testexpression1“(+)”)”并获得“12”::Int
我知道代码没有意义,但它是一个家庭作业,我想用haskell尝试一些东西。希望你有比我更多的想法。
对不起,我的英语不好,我已经开始学习英语,但我才刚刚开始,谢谢你的每一个想法和一切。
编辑,这是代码中缺少的内容:
import Test.HUnit (runTestTT,Test(TestLabel,TestList),(~?))
import Data.Function (on)
import Language.Haskell.Interpreter -- Hint package
import Data.Text
import Data.Text.Encoding
import Data.ByteString (ByteString)
import Control.Monad.Catch
-- | A very simple data type for expressions.
data Expr = Const Int | Add Expr Expr deriving Show
-- | 'Expression' is an instance of 'Num'. You will get warnings because
-- many required methods are not implemented.
instance Num Expr where
fromInteger = Const . fromInteger
(+) = Add
-- | Equality of 'Expr's modulo associativity.
instance Eq Expr where
(==) x1 x2 = True --(helper x1 "(+)") == (helper x2 "(+)") && (helper x1 "(*)") == (helper x2 "(*)")
这些函数也在文件中......我文件中的所有其他内容都是我为我创建的一些测试用例。