Skip to content

Instantly share code, notes, and snippets.

@5outh
5outh / eval.hs
Last active December 16, 2015 20:10
evaluation functions
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)
@5outh
5outh / taylorseries.hs
Last active December 16, 2015 20:10
taylor series representation of a function
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
@5outh
5outh / ddxs.hs
Last active December 16, 2015 20:19
More derivative stuff
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)
@5outh
5outh / DeMorgan.hs
Created November 13, 2013 17:04
DeMorgan
{-# 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
@5outh
5outh / BoolExpr.hs
Last active December 28, 2015 06:19
data Expr = Not Expr
| And Expr Expr
| Or Expr Expr
| SubExpr Expr
| Var Char
deriving Eq
@5outh
5outh / not.hs
Created November 13, 2013 20:58
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)
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"
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 ++ ")"
main = mapM_ printNotExpr . lines =<< readFile "inputs.txt"
where printNotExpr e = case parseExpr e of
Right x -> print $ not x
Left e -> error $ show e
@5outh
5outh / Bots.hs
Last active August 17, 2018 12:49
Bots2 Clone
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)