whats kind of Error is this?
error:Variable not in scope: guard :: Bool -> Maybe a0
thats the Code
eval3 :: Exp -> Maybe Integer
eval3 e =
case e of
(Const x) -> pure x
(ExpAdd x y) -> eval3 x >>= \x' -> eval3 y >>= \y' -> return (x' y')
(ExpMult x y) -> eval3 x >>= \x' -> eval3 y >>= \y' -> return (x'*y')
(ExpDiv x y) -> eval3 x >>= \x' -> eval3 y >>= (\y' -> guard (y'/=0) >> return (div x'y'))
CodePudding user response:
guard
is in Control.Monad
, not Prelude
. You just need to import Control.Monad
at the top of your file.
CodePudding user response:
As @SilvioMoyolo says, you need to import Control.Monad
. Howeve, using guard
looks a bit like overkill here, you can work with a guard instead. FOr the ExpAdd
and ExpMult
, you can use liftA2
:
import Control.Applicative(liftA2)
import Data.Function(on)
eval3 :: Exp -> Maybe Integer
eval3 (Const x) = pure x
eval3 (ExpAdd x y) = (liftA2 ( ) `on` eval3) x y
eval3 (ExpMult x y) = (liftA2 (*) `on` eval3) x y
eval3 (ExpDiv x y)
| Just x' <- eval3 x, Just y' <- eval3 y, y' /= 0 = pure (div x' y')
| otherwise = Nothing