String 如何检查分析是否失败?
我希望继续对字符串应用解析器,并添加其输出,直到到达字符串的末尾。不幸的是,当我解析字符串的结尾时,它失败了,所以我得到了一个空列表。我希望能够检查解析是否失败,然后从失败解析之前的所有解析中获取输出。我试图将这个字符串解析为一个浮点列表。我如何才能阻止这种情况发生,即获得一个空列表而不是我的浮动列表 我已经用这种类型编写了一个解析器:newtype parser a=MkP String->[String,a] 它的monad实例是:String 如何检查分析是否失败?,string,parsing,haskell,String,Parsing,Haskell,我希望继续对字符串应用解析器,并添加其输出,直到到达字符串的末尾。不幸的是,当我解析字符串的结尾时,它失败了,所以我得到了一个空列表。我希望能够检查解析是否失败,然后从失败解析之前的所有解析中获取输出。我试图将这个字符串解析为一个浮点列表。我如何才能阻止这种情况发生,即获得一个空列表而不是我的浮动列表 我已经用这种类型编写了一个解析器:newtype parser a=MkP String->[String,a] 它的monad实例是: -- (>>=) :: m a -> (
-- (>>=) :: m a -> (a -> m b) -> m b
instance Monad Parser where
return x = MkP f
where
f inp = [(inp,x)]
f >>= g = MkP s
where
s inp = [(rest,out) | (firstremain,firstout) <- applyParser f inp, (rest,out) <- applyParser (g firstout) firstremain]
我正在尝试分析此字符串:
unparsedboeyield = "0.63 0.81 1.01 1.20 1.38 1.54 1.68 1.79 1.89 1.97 2.05 2.11 2.17 2.23 2.28 2.33 2.37 2.42 2.45 2.49 2.53 2.56 2.59 2.62 2.64 2.67 2.69 2.71 2.73 2.75 2.77 2.79 2.80 2.82 2.83 2.84 2.85 2.87 2.87 2.88 2.89 2.90 2.90 2.91 2.91 2.92 2.92 2.92 2.93 2.93"
使用此解析器:
numberParser :: Parser Float
numberParser = do
a <- item
b <- item
c <- item
d <- item
let number = read [a,b,c,d] :: Float in return number
yieldParser :: Parser [Float]
yieldParser = do
x <- numberParser
helper [x] where
helper y = do
a <- randomthing
helper (y ++ [a])
randomthing = do
item
item
item
item
numberParser
当一件事情失败时,什么是一个错误
当randomthing生成一个空列表时,a从不获取值
如果查看>>=的定义,对于applyParser f inp中的每个结果,g都应用于firstout。所以,如果applyParser f inp产生零结果,g被应用零次
在helper的定义中,f是randomthing,g是\a->helper y++[a]。因此,如果g从未被应用,当randomthing为给定的输入生成一个空列表时,a就没有值,因为a只存在于g内部
我希望能够检查分析是否失败
你不能用一元接口。相反,您必须自己使用applyParser,然后检查它是否返回空列表
numberParser :: Parser Float
numberParser = do
a <- item
b <- item
c <- item
d <- item
let number = read [a,b,c,d] :: Float in return number
yieldParser :: Parser [Float]
yieldParser = do
x <- numberParser
helper [x] where
helper y = do
a <- randomthing
helper (y ++ [a])
randomthing = do
item
item
item
item
numberParser
import Control.Monad
import Data.Char
interpolate :: (Ord a, Fractional a) => ((a,a),(a,a)) -> a -> a
interpolate ((x1,y1),(x2,y2)) x = if (x > x2) || (x < x1) then error "value out of range" else y1 + difference * gradient
where
gradient = (y1 -y2)/(x1 - x2)
difference = x - x1
pvt :: Fractional a => a -> Int -> a
pvt x t = x / (m + spotrate t)^t where m = 1 :: Fractional a => a
unparsedboeyield :: String
unparsedboeyield = "0.63 0.81 1.01 1.20 1.38 1.54 1.68 1.79 1.89 1.97 2.05 2.11 2.17 2.23 2.28 2.33 2.37 2.42 2.45 2.49 2.53 2.56 2.59 2.62 2.64 2.67 2.69 2.71 2.73 2.75 2.77 2.79 2.80 2.82 2.83 2.84 2.85 2.87 2.87 2.88 2.89 2.90 2.90 2.91 2.91 2.92 2.92 2.92 2.93 2.93"
newtype Parser a = MkP (String -> [(String,a)])
applyParser :: Parser a -> String -> [(String,a)]
applyParser (MkP x) y = x y
-- (>>=) :: m a -> (a -> m b) -> m b
instance Monad Parser where
return x = MkP f
where
f inp = [(inp,x)]
f >>= g = MkP s
where
s inp = [(rest,out) | (firstremain,firstout) <- applyParser f inp, (rest,out) <- applyParser (g firstout) firstremain]
item :: Parser Char
item = MkP f
where
f [] = []
f (x:xs) = [(xs,x)]
zero :: Parser Char
zero = MkP f
where
f _ = []
sat :: (Char -> Bool) -> Parser Char
sat predicate =
item >>= \x ->
if predicate x then return x else zero
doParser :: Parser a -> Int -> Parser ()
doParser x count = helper count
where
helper count = do
if count > 0 then do
x
helper (count - 1)
else return ()
numberParser :: Parser Float
numberParser = do
a <- item
b <- item
c <- item
d <- item
let number = read [a,b,c,d] :: Float in return number
yieldParser :: Parser [Float]
yieldParser = do
x <- numberParser
helper [x] where
helper y = do
a <- randomthing
helper (y ++ [a])
randomthing = do
item
item
item
item
numberParser
spotrate :: Fractional a => Int -> a
spotrate = \t -> if (t == 1) then 5 else 2
untilParser :: (Char -> Bool) -> Parser [Char]
untilParser p = helper []
where
helper x = do
y <- item
if p y then helper (x ++ [y]) else return x
helper y = do
a <- randomthing
helper (y ++ [a])