我觉得很难学秒差距在Haskell,所以我试图让我的大学项目(即分析文件与表格字符串变量名哈斯克尔
x=3
y=4
z=x+y
badluck=(x+sqrt(z)*7)
我好不容易写一个函数解析器从文件中获取所有内容并验证文件,并且我被困在使x成为变量名的情况下,我知道在javascript中它是eval,但我在Haskell中找不到任何类似的东西。直到现在:
ischarorscore :: Char -> Bool
ischarorscore a = if ((a>='A' && a<='Z') || (a>='a' && a<='z') || a=='_')
then True
else False
ischarscoredigit :: Char -> Bool
ischarscoredigit a = if ((a>='A' && a<='Z') ||
(a>='a' && a<='z') ||
a=='_' ||
a>='0' && a<='9') then True else False
isvar :: String -> Bool
isvar [] = False
isvar (h:t) = if (ischarorscore h) then (isvarbody t) else False
isvarbody :: String -> Bool
isvarbody [] = True
isvarbody (h:t) = if (ischarscoredigit h) then (isvarbody t) else False
isoperator :: Char -> Bool
isoperator a = if (a=='+' || a=='-' || a=='*' || a=='/' || a=='^') then True else False
issqrt :: String -> Bool
issqrt [] = True
issqrt x = if (x=="sqrt(") then True else False
isnumber :: String -> Bool
isnumber (h:t) = if (h>='0' && h<='9') then isnumber t else False
charsetall :: String -> Bool
charsetall [] = True
charsetall (h:t) = if (h>='0' && h<='9' ||
h>='a' && h<='z' ||
h>='A' && h<='Z' ||
h>='0' && h<='9' ||
h=='+' || h=='-' || h=='*' || h=='/' || h=='^' || h=='(' || h==')' || h=='=')
then charsetall t else False
charsetexpr :: String -> Bool
charsetexpr [] = True
charsetexpr (h:t) = if (h>='0' && h<='9' ||
h>='a' && h<='z' ||
h>='A' && h<='Z' ||
h>='0' && h<='9' ||
h=='+' || h=='-' || h=='*' || h=='/' || h=='^' || h=='(' || h==')')
then charsetexpr t else False
paranthesis :: String -> Int -> Bool
paranthesis [] a = if (a==0) then True else False
paranthesis (h:t) a = if (h=='(') then (paranthesis t (a+1))
else (if (h==')') then
paranthesis t (a-1) else paranthesis t a)
paranthesis' :: String -> Bool
paranthesis' (h:t) = paranthesis (h:t) 0
obeyrules :: String -> Bool
obeyrules [] = True
obeyrules (h:t) = if (ischarorscore h) then (contvar t)
else if (h>='0' && h<='9') then (contnumber t)
else if (h=='(' || h=='-') then obeyrules t
else False
contnumber :: String -> Bool
contnumber [] = True
contnumber (h:t) = if (h>='0' && h<='9') then contnumber t
else if (isoperator h) then contoperator t
else if (h==')') then contoperator h
else False
contvar :: String -> Bool
contvar [] = True
contvar (h:t) = if (ischarorscore h || h>='0' && h<='9') then contvar t
else if (isoperator h) then contoperator t
else if (h==')') then contoperator
else False
contoperator :: String -> Bool
contoperator [] = True
contoperator (h:t) = if (ischarorscore h) then contvar t
else if (h>='0' && h<='9') then contnumber t
else if (h=='(') then obeyrules t
else False
isexpression :: String -> Bool
isexpression [] = True
isexpression (h:t) = if (charsetexpr (h:t) && paranthesis' (h:t) && obeyrules (h:t)) then True else False
isexpression函数结合了上述所有功能来验证文件的一行。该项目是这样的:你必须阅读一个随机文件,看起来像上面那样,并包含变量名称=表达式和识别(,),+, - ,*,/,^这是功率,sqrt(k)其中k是数字或变量名称,mod,计算表达式并提供结果变量名称及其值。
不明确的问题。你能不能改写你完成了什么(代码,也许?)以及你想达到的目标(例如输入和输出对)? –
@larsmans'ghci'叫:)(告诉你关于['系统。Eval.Haskell'](http://hackage.haskell.org/package/plugins-1.5.1.4/docs/System-Eval-Haskell.html)) –
你可能正在寻找一名口译员,目前还不清楚。考虑使用提示,例如:http://stackoverflow.com/questions/5582926/haskell-how-to-evaluate-a-string-like-12/5584638#5584638 –