use*_*038 9 haskell text-parsing
我想要一个看起来像这样的函数
readFunc :: String -> (Float -> Float)
Run Code Online (Sandbox Code Playgroud)
它的运作方式是这样的
>(readFunc "sin") (pi/2)
>1.0
>(readFunc "(+2)") 3.0
>5.0
>(readFunc "(\x -> if x > 5.0 then 5.0 else x)") 2.0
>2.0
>(readFunc "(\x -> if x > 5.0 then 5.0 else x)") 7.0
>5.0
Run Code Online (Sandbox Code Playgroud)
非常天真的方法(注意这必须编译{-# LANGUAGE FlexibleContexts #-})
readFunc :: (Read (Float -> Float)) => String -> (Float -> Float)
readFunc s = read s
Run Code Online (Sandbox Code Playgroud)
给
No instance for (Read (Float -> Float)) ...
Run Code Online (Sandbox Code Playgroud)
这是有道理的,因为不存在这样的实例.我理解我可以通过编写一个映射来逐字符地解析输入字符串String,Float -> Float但是我希望能够至少解析前奏中最常见的函数,甚至这将比我想要的工作更多.有这么简单的方法吗?
import Language.Haskell.Interpreter hiding (typeOf)
import Data.Typeable (typeOf)
data Domain = Dom Float Float Float Float Domain
| SDom Float Float Float Float
deriving (Show, Read)
--gets all the points that will appear in the domain
points (SDom a b c d) m = [(x, y)|x <- [a, a+m .. b], y <- [c, c+m .. d]]
points (Dom a b c d next) m = points next m ++ [(x, y)|x <- [a, a+m .. b], y <- [c, c+m .. d]]
readFunc = do
putStrLn "Enter a domain (as Dom x-min x-max y-min y-max subdomain, or, SDom x-min x-max y-min y-max)"
domain' <- getLine
let domain = (read domain') :: Domain
--
putStrLn "Enter a mesh size"
meshSize' <- getLine
let meshSize = (read meshSize') :: Float
--
putStrLn "Enter an initial value function (as f(x,y))"
func' <- getLine
values' <- runInterpreter $ setImports["Prelude"] >>
eval ("map (\\(x,y) -> " ++ func' ++ ")" ++ show (points domain meshSize))
let values = (\(Right v) -> (read v)::([Float])) values'
--the haskell expression being evaluated
putStrLn $ ("map (\\(x,y) -> " ++ func' ++ ")" ++ show (points domain meshSize))
--prints the actual values
putStrLn $ show values
--the type is indeed [float]
putStrLn $ show $ typeOf values
Run Code Online (Sandbox Code Playgroud)
您可以使用提示包或插件.我会告诉你前者(部分是因为我的Windows安装显然有点破坏,因为cabal并不认为我安装了C,所以cabal安装插件失败).
import Language.Haskell.Interpreter
getF :: String -> IO (Either InterpreterError (Float -> Float))
getF xs = runInterpreter $ do
setImports ["Prelude"]
interpret xs (as :: Float -> Float)
Run Code Online (Sandbox Code Playgroud)
您可能希望将其他模块添加到导入列表中.这测试为
ghci> getF "sin" >>= \(Right f) -> print $ f (3.1415927/2)
1.0
ghci> getF "(\\x -> if x > 5.0 then 5.0 else x)" >>= \(Right f) -> print $ f 7
5.0
Run Code Online (Sandbox Code Playgroud)
(注意逃脱字符的转义\.)
您可能已经注意到,结果包含在Either数据类型中.Right f是正确的输出,而Left err给出一个InterpreterError消息,这是非常有用的:
ghci> getF "sinhh" >>= \(Left err) -> print err
WontCompile [GhcError {errMsg = "Not in scope: `sinhh'\nPerhaps you meant `sinh' (imported from Prelude)"}]
Run Code Online (Sandbox Code Playgroud)
当然,您可以使用either您的代码来处理这个问题.让我们做一个假的例子respond.你真实的将包含你的程序的所有数学.
respond :: (Float -> Float) -> IO ()
respond f = do
-- insert cunning numerical method instead of
let result = f 5
print result
Run Code Online (Sandbox Code Playgroud)
一个简单的,一次尝试,无益的程序版本就可以了
main =
putStrLn "Enter your function please:"
>> getLine
>>= getF
>>= either print respond
Run Code Online (Sandbox Code Playgroud)
ghci> main
Enter your function please:
\x -> x^2 + 4
29.0
Run Code Online (Sandbox Code Playgroud)
ghci> main
Enter your function please:
ln
WontCompile [GhcError {errMsg = "Not in scope: `ln'"}]
Run Code Online (Sandbox Code Playgroud)
它会为您进行类型检查:
ghci> main
Enter your function please:
(:"yo")
WontCompile [GhcError {errMsg = "Couldn't match expected type `GHC.Types.Float'\n with actual type `GHC.Types.Char'"}]
Run Code Online (Sandbox Code Playgroud)