Skip to content

Instantly share code, notes, and snippets.

@josephcsible
josephcsible / fib.hs
Created November 9, 2024 07:20
Closed-form Fibonacci numbers
import Data.Maybe (fromJust)
import Data.Ratio ((%), numerator, denominator)
data FibHelper = FibHelper Rational Rational deriving(Eq,Read,Show)
compareFibHelperWithZero :: FibHelper -> Ordering
compareFibHelperWithZero (FibHelper a b) = (a * abs a) `compare` (-5 * b * abs b)
instance Ord FibHelper where
x `compare` y = compareFibHelperWithZero (x - y)
@josephcsible
josephcsible / q73762024.hs
Last active September 19, 2022 13:12
Using the C preprocessor with single quotes where C wouldn't allow them
{-# LANGUAGE CPP #-}
#define MAIN_FN_NAME main
#define puts putStrLn hello'world
hello'world :: String
hello'world = "Hello, World!"
MAIN_FN_NAME = puts
@josephcsible
josephcsible / README.md
Last active July 19, 2022 04:05
Masquerade one Wi-Fi network to another from a Mac
  • Boot with a USB drive with an image from https://github.com/marcosfad/mbp-ubuntu
  • Hold Option at the chime and pick the last one
  • echo high > /sys/bus/pci/drivers/amdgpu/0000:??:??.?/power_dpm_force_performance_level
  • apt install /usr/src/iso-firmware.deb
  • modprobe -r brcmfmac
  • modprobe brcmfmac
  • systemctl restart network-manager
  • Add this to /etc/NetworkManager/NetworkManager.conf:
[device]
global memcpy64, _memcpy64
section .text
memcpy64:
_memcpy64:
push edi
push esi
call 0x33:.heavensgate
pop esi
pop edi
@josephcsible
josephcsible / parentconsole.c
Created June 3, 2022 17:47
Write to the parent's console regardless of process creation flags
#include <windows.h>
#include <stdio.h>
int main(void) {
FreeConsole();
AttachConsole(ATTACH_PARENT_PROCESS);
freopen("CONOUT$", "w", stdout);
puts("This will go to the parent's console no matter what process creation flags were used");
}
@josephcsible
josephcsible / DebianVersionString.hs
Last active May 27, 2022 03:58
A comparator for upstream_version and debian_revision strings, as described at https://www.debian.org/doc/debian-policy/ch-controlfields.html#version
{-# LANGUAGE DerivingStrategies, GeneralizedNewtypeDeriving, TypeApplications #-}
import Data.Char
import Data.Function
newtype DebianVersionString = DebianVersionString String deriving newtype Show
instance Eq DebianVersionString where
x == y = compare x y == EQ
import Control.Monad.State
import Data.Traversable
{-
>>> uglyMap (,,) "abc"
[('a',0,"abc"),('b',1,"abc"),('c',2,"abc")]
-}
uglyMap :: Traversable t => (a -> Int -> t a -> b) -> t a -> t b
uglyMap f xs = flip evalState 0 $ for xs $ \x -> do
i <- get
@josephcsible
josephcsible / Main.hs
Last active June 2, 2020 21:05
"Obvious" refactors can be wrong
{-# OPTIONS -Werror=type-defaults #-}
import Data.Maybe (isJust)
import Data.Word (Word16, byteSwap16)
default ()
oneNumTup :: Num a => (a, a)
oneNumTup = (2, 300)
Var ::= [a-zA-Z]+
;
PrimaryTerm ::= Var
| '(' Term ')'
;
ApplyTerm ::= PrimaryTerm
| ApplyTerm PrimaryTerm
;
@josephcsible
josephcsible / Counterexample.hs
Last active December 29, 2019 18:48
Something like STRef but usable in a monad transformer
{-# LANGUAGE RankNTypes #-}
-- Adapted from https://gist.github.com/Lysxia/18fce5d4ce8ff7eb7117dfb827770187 to work on StatesT
import Control.Monad
import Control.Monad.Trans.Class
import Data.Void
import StatesT
-- Demonstration that StatesT is unsafe