我覺得很難學秒差距在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 –