This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
evalExpr :: (Num a, Floating a) => Char -> a -> Expr a -> a | |
evalExpr c x = evalExpr' . plugIn c x | |
evalExpr' :: (Num a, Floating a) => Expr a -> a | |
evalExpr' (Const a) = a | |
evalExpr' (Var c) = error $ "Variables (" | |
++ [c] ++ | |
") still exist in formula. Try plugging in a value!" | |
evalExpr' (a :+: b) = (evalExpr' a) + (evalExpr' b) | |
evalExpr' (a :*: b) = (evalExpr' a) * (evalExpr' b) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
taylor :: (Floating a, Eq a) => Expr a -> [Expr a] | |
taylor expr = fmap fullSimplify (fmap series exprs) | |
where indices = fmap fromIntegral [1..] | |
derivs = fmap (changeVars 'a') (ddxs expr) | |
where changeVars c = mapVar (\_ -> Var c) | |
facts = fmap Const $ scanl1 (*) indices | |
exprs = zip (zipWith (:/:) derivs facts) indices -- f^(n)(a)/n! | |
series (expr, n) = | |
expr :*: ((Var 'x' :+: (negate' $ Var 'a')) :^: Const n) -- f^(n)(a)/n! * (x - a)^n |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ddx :: (Floating a, Eq a) => Expr a -> Expr a | |
ddx = fullSimplify . derivative | |
ddxs :: (Floating a, Eq a) => Expr a -> [Expr a] | |
ddxs = iterate ddx | |
nthDerivative :: (Floating a, Eq a) => Int -> Expr a -> Expr a | |
nthDerivative n = foldr1 (.) (replicate n ddx) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE NoMonomorphismRestriction #-} | |
import Text.ParserCombinators.Parsec | |
import Text.ParserCombinators.Parsec.Token hiding (parens) | |
import Text.ParserCombinators.Parsec.Expr | |
import Control.Applicative hiding ((<|>)) | |
import Control.Monad | |
import Prelude hiding (not) | |
data Expr = Not Expr | And Expr Expr | Or Expr Expr | Var Char | SubExpr Expr deriving Eq |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data Expr = Not Expr | |
| And Expr Expr | |
| Or Expr Expr | |
| SubExpr Expr | |
| Var Char | |
deriving Eq |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
not :: Expr -> Expr | |
not (Not e) = e | |
not (And e1 e2) = Or (not e1) (not e2) | |
not (Or e1 e2) = And (not e1) (not e2) | |
not (SubExpr e) = not e | |
not (Var c) = Not (Var c) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
parseExpr :: String -> Either ParseError Expr | |
parseExpr = parse expr "" | |
where expr = buildExpressionParser operators term <?> "compound expression" | |
term = parens expr <|> variable <?> "full expression" | |
operators = [ [Prefix (string "NOT" *> spaces *> pure Not)] | |
, [binary "AND" And] | |
, [binary "OR" Or] ] | |
where binary n c = Infix (string n *> spaces *> pure c) AssocLeft | |
variable = Var <$> (letter <* spaces) <?> "variable" | |
parens p = SubExpr <$> (char '(' *> spaces *> p <* char ')' <* spaces) <?> "parens" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
instance Show Expr where | |
show (Not e) = "NOT " ++ show e | |
show (And e1 e2) = show e1 ++ " AND " ++ show e2 | |
show (Or e1 e2) = show e1 ++ " OR " ++ show e2 | |
show (Var c) = [c] | |
show (SubExpr e) = "(" ++ show e ++ ")" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
main = mapM_ printNotExpr . lines =<< readFile "inputs.txt" | |
where printNotExpr e = case parseExpr e of | |
Right x -> print $ not x | |
Left e -> error $ show e |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Pipes | |
import qualified Pipes.Prelude as P | |
import qualified System.Random as R | |
import Lens.Family2 | |
import Lens.Family2.Stock | |
import Lens.Family2.State.Lazy | |
import Control.Monad.Trans.State | |
import Control.Monad | |
import Control.Concurrent(threadDelay) |