Skip to content

Instantly share code, notes, and snippets.

View hyphenrf's full-sized avatar

Haz hyphenrf

  • Alexandria, Egypt
View GitHub Profile
let reinterpret_float (f : float) : int64 = Obj.(
let i = repr 0L
and f = repr f
in
set_field i 1 (field f 0);
obj i
)
let reinterpret_float = Int64.bits_of_float
#define wwx ____""
#include /**/<stdio.h>
int p ( int ch){
printf ("%c"
,ch); } int
main()
{ char
h [] =
"hel"\
/*
Today it was asked on discord whether Haskell's typeclasses and Rust's traits are equivalent.
The answer came from a Haskeller that:
> Yes, they’re along the same lines.
> Haskell’s typeclasses are more powerful than Rust’s traits though
I found that claim a little dubious so I asked them to elaborate.. How are traits less powerful?
To which a friend of mine suggested, maybe because Rust doesn't offer functional dependencies.
I didn't see why they can't be emulated with existent features. So I replied with that,
#!/usr/bin/es
tests = (
\e[1mbold\e[0m
\e[2mdimmed\e[0m
\e[3mitalic\e[0m
\e[4munderlined\e[0m
\e[5mslowblink\e[0m
\e[6mrapidblink\e[0m
\e[7minverted\e[0m
@hyphenrf
hyphenrf / ramanujan.hs
Created February 4, 2022 00:34
cute and reasonably fast lazy ramanujan numbers
zipper xs = ([], xs)
next (xs, y:ys) = (y:xs, ys)
next z = z
rmj n = take n naive
where
naive = go $ zipper [ x^3 + y^3 | x <- [1..], y <- [1..x] ]
go (_,[]) = []
go xs@(l,n:_) = let ns = go $ next xs in
@hyphenrf
hyphenrf / zippers.hs
Last active December 12, 2022 17:22
derivatives of ADTs are their zippers
-- list derivation
-- data [a] = [] | a : [a]
{-
l = 1 + a ⋅ l
l - a ⋅ l = 1
l (1 - a) = 1
l = (1 - a)⁻¹
∂l = -1 ⋅ -(1 - a)⁻²
@hyphenrf
hyphenrf / gadt_of_prolog.ml
Last active October 10, 2022 23:00
this actually works :D
(*
nat(z).
nat(s(N)) :- nat(N).
plus(N, z, N).
plus(N, s(M), s(A)) :- plus(N, M, A).
mult(_, z, z).
mult(N, s(M), B) :- mult(N, M, A), plus(N, A, B).
@hyphenrf
hyphenrf / choose.curry
Last active January 27, 2022 15:10
nondeterministic choice implemented in different languages
{-# OPTIONS_FRONTEND -Wno-overlapping #-}
import AllSolutions
insert :: a -> [a] -> [a] -- warning: overlapping patterns = ndet
insert x xs = x : xs
insert x (y:ys) = y : insert x ys
perm :: [a] -> [a]
perm [] = []
@hyphenrf
hyphenrf / show.c
Last active January 1, 2022 19:30
#define show(fmt, ...) (printf("%s: ", #__VA_ARGS__), printf(fmt, __VA_ARGS__))
#define show8(x) printf(#x":\t%02hhx (%hhd)\n", x, x)
@hyphenrf
hyphenrf / combinations.hs
Created June 15, 2021 11:28
you've heard of permutations, get ready for this
-- possibly the worst implementation out there but
combinations [] = []
combinations xs = go (length xs) (map pure xs) xs where
go 1 zs __ = zs
go n zs ys = [ y:z | y <- ys, z <- go (n-1) zs ys ]