Skip to content

Instantly share code, notes, and snippets.

View dmwit's full-sized avatar

Daniel Wagner dmwit

View GitHub Profile
@dmwit
dmwit / Dd.txt
Last active July 8, 2019 21:25
automatic differentiation comment
In the Essence of Automatic Differentiation, Conal defines
D+ :: (a -> b) -> a -> (b, a -o b)
D+(f,a) = (f(a), D(f,a))
where a and b are vector spaces, -o is the space of linear functions, and D is
the derivative operation.
But to me this suffers the same problem as considering numbers to be the
derivatives of 1D spaces functions, vectors/matrices for higher dimensions,
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
import Type.Reflection
data Foo = Int Int | Bool Bool
wrap :: Typeable b => b -> Maybe Foo
wrap b
| Just HRefl <- eqTypeRep t (typeRep @Int) = Just (Int b)
| Just HRefl <- eqTypeRep t (typeRep @Bool) = Just (Bool b)
sums :: Num a => [a] -> (a, [a])
sums [] = (0, [])
sums (x:xs) = let ~(v,vs) = sums xs; v' = v+x in v' `seq` (v',v':vs)
main :: IO ()
main = print (fst (sums [0..1000000 :: Int]))
-- OR
-- main = print (last (scanl1 (+) [0..1000000 :: Int]))
-- Test.hs
{-# LANGUAGE PatternSynonyms #-}
module Test (Foo(Bar,Baz)) where
data Foo = Bar
pattern Baz = Bar
-- whatever-else.hs
import Test (Foo(Baz))
@dmwit
dmwit / terminal.txt
Created August 17, 2019 19:23
what am I doing wrong?
% tree
.
├── a
│   └── a.cabal
└── b
├── b.cabal
└── cabal.project.local
2 directories, 3 files
% cat a/a.cabal
> mapM_ (print . pretty) $ runInfer (SourceApp (SourceApp (SourceVar "plus") (SourceVar "readLn")) (SourceVar "readLn"))
let appPMPPM f x = pure (f (pure x))
appMaMaM f x = f >>= ($x)
appMMMPM f x = f >>= ($pure x)
appMPMMM f x = f >>= (x>>=)
appMaPaM f x = fmap ($x) f
appMMPPM f x = fmap ($pure x) f
in
(<*>) (fmap plus readLn) readLn
:: m _
@dmwit
dmwit / Custom.hs
Created September 13, 2019 23:45
a better tutorialspoint
module Custom (
showEven,
showBoolean
) where
showEven:: Int-> Bool
showEven x = x `rem` 2 == 0
showBoolean :: Bool->Int
showBoolean c = if c then 1 else 0
c (1, 1) = 1
c (2, 2) = 1
c (i, 1) | i > 2 = -(a ! (i-2, 1))
c (i, j) | i == j = 2 * (a ! (i-1, j-1))
| i > j && j > 1 = 2 * (a ! (i-1, j-1)) - (a ! (i-2, j-1))
| otherwise = 0
@dmwit
dmwit / idklol
Created September 28, 2019 03:15
derangements :: Integer -> Integer
derangements n = id
. sum
. zipWith (*) signs
. scanl (*) 1
$ [n,n-1..1]
where
signs = drop (n .&. 1) (cycle [1,-1])
newtype A = A Int
newtype B = B Int
type Database = String
type JSON = String
class ParseDatabase a where parse :: Database -> Maybe a
instance ParseDatabase A where parse db = A <$> readMaybe s
instance ParseDatabase B where parse db = B <$> readMaybe s