如何在Haskell中将字符串转换为枚举数据类型?

我有一个像这样的枚举数据类型:

data MathExpr a = X
               | Coef a
               | Sum (MathExpr a) (MathExpr a)
               | Prod (MathExpr a) (MathExpr a)
               | Quot (MathExpr a) (MathExpr a)
               | Exp (MathExpr a)
               | Log (MathExpr a)
                 deriving (Eq,Show,Read)

我正在尝试将String转换为这种类型。我想使用前奏中提供的读取功能。为此,我创建了另一个函数:

readMathExpr :: String -> MathExpr a
readMathExpr = read

这在编译时给我一个错误,说没有使用(read)引起的(Read a)实例。如果有人能指出正确的方向或链接与阅读功能有关的有用教程,我将不胜感激。谢谢 !

youcansayyes 回答:如何在Haskell中将字符串转换为枚举数据类型?

问题很简单,您提议的类型签名过于笼统。 @echo off if "%~1"=="startchk" goto chk start "" cmd /c "%~f0 startchk" rem Rest of code here goto :eof :chk set count=0 for /f "tokens=1 delims= " %%a in ('tasklist ^| findstr /r "[c][m][d][.][e][x][e]"') do set /a count += 1 :chk2 set new=0 for /f "tokens=1 delims= " %%a in ('tasklist ^| findstr /r "[c][m][d][.][e][x][e]"') do set /a new += 1 if %new% lss %count% goto over goto chk2 :over msg * "You have closed cmd" Goto :eof 的{​​{1}}的派生实例仅在Read已有MathExpr a的实例时才起作用。也就是说,如果您要自己写出实例,它将开始:

Read

我不认为在实践中您不希望为类型a读取类型为instance (Read a) => Read (MathExpr a) where ... 的值MathExpr a本身不是a的实例,因此解决方法只是在签名中添加必要的类型类约束:

Read
本文链接:https://www.f2er.com/3143151.html

大家都在问