Skip to content

Instantly share code, notes, and snippets.

View ayanamists's full-sized avatar

LI CHENXI ayanamists

View GitHub Profile
@ayanamists
ayanamists / AD.hs
Created June 7, 2024 03:56 — forked from ttesmer/AD.hs
Automatic Differentiation in 38 lines of Haskell using Operator Overloading and Dual Numbers. Inspired by conal.net/papers/beautiful-differentiation
{-# LANGUAGE TypeSynonymInstances #-}
data Dual d = D Float d deriving Show
type Float' = Float
diff :: (Dual Float' -> Dual Float') -> Float -> Float'
diff f x = y'
where D y y' = f (D x 1)
class VectorSpace v where
zero :: v
@ayanamists
ayanamists / cartesian_tree.hs
Created May 23, 2024 02:31 — forked from evgenii-malov/cartesian_tree.hs
Build cartesian tree in Haskell in 3 ways (O(N^2), O(N*LogN) and O(N))
-- video https://youtu.be/E8Fxtpr24Zg
-- GHCi, version 8.8.4
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeOperators #-}
{-# OPTIONS_GHC -Wno-incomplete-patterns #-}
import PrettyT -- https://www.youtube.com/watch?v=Ud-1Z0hBlB8&t=379s
-- data Btree a = Empty | Node a (Btree a) (Btree a) deriving Show
import Data.Ord
import Data.List