Skip to content

Instantly share code, notes, and snippets.

// https://twitter.com/e869120/status/1741475801026204116#
(*
(20 + (((23 + 12) - 31) * ((20 * (24 + 1)) + 1)))
(((20 * 23) + (((12 + 31) - 20) * 24)) * (1 + 1))
*)
let rec gcd a (b: int) = if b = 0 then a else gcd b (a % b)
@pocarist
pocarist / FizzBuzz.fs
Created September 26, 2023 16:04
FizzBuzz written using F# sequence
let fizz =
Seq.initInfinite (fun i -> if (i+1) % 3 = 0 then "Fizz" else "")
let buzz =
Seq.initInfinite (fun i -> if (i+1) % 5 = 0 then "Buzz" else "")
let fizzbuzz =
Seq.mapi2 (fun i f b -> match f + b with "" -> string (i + 1) | x -> x) fizz buzz
fizzbuzz
|> Seq.take 100
|> Seq.iter (printfn "%s")
@pocarist
pocarist / heap.ml
Created January 29, 2016 08:19
Meldable Heap (OCaml version)
module type OrderedType = sig
type t
val compare : t -> t -> int
end
module type S =
sig
type elt
type t
val empty: t
open System
let memo_rec f =
let m = ref Map.empty in
let rec g x =
match Map.tryFind x !m with
| Some ans -> ans
| None ->
let y = f g x in
m := Map.add x y !m
@pocarist
pocarist / primes.cpp
Created September 15, 2015 03:35
Sieve of Eratosthenes
#include <vector>
#include <cmath>
using namespace std;
vector<int> primes(int n)
{
vector<bool> table(n + 1, true);
table[0] = table[1] = false;
int mid = (int)floor(sqrt(n));
for (int i = 2; i <= mid; i++) {
@pocarist
pocarist / heap.fs
Created July 18, 2015 08:15
Meldable Heap
module Heap =
type tree<'T> = Node of 'T * tree<'T> * tree<'T>
| Leaf
type t<'T> = { root : tree<'T>
cmp : 'T -> 'T -> bool
}
let create cmp =
{root=Leaf; cmp=cmp}
let rec meld cmp a b =
@pocarist
pocarist / has_mem_xxx.cpp
Created May 29, 2015 14:57
sample of has_mem_xxx.
// sample of has_mem_xxx.
// this can be compiled with MS VC2012.
#include <iostream>
#include <utility>
#include <type_traits>
template< typename T >
struct has_mem_xxx {
template< typename U > static std::true_type check( decltype( std::declval<U>().xxx )* );
// http://www.prefield.com/algorithm/container/fenwick_tree.html
// add(k, a) == v[k] = v[k] + a
// sum(i, j) == v[i] + ... + v[j] (両端を含む)
type FenwickTree(N : int) =
let zero = LanguagePrimitives.GenericZero
let x = Array.create N zero
member me.sum i j =
if i = 0 then
let rec loop acc k =
if k >= 0 then loop (acc + x.[k]) ((k &&& (k+1)) - 1)
@pocarist
pocarist / signals_osdep.h.patch
Last active August 29, 2015 14:14
Fix compile error for NetBSD/i386 (OCaml 4.02 or later)
--- ocaml-4.02.1/asmrun/signals_osdep.h 2014-09-29 04:46:24.000000000 +0900
+++ ocaml-4.02.1_fix/asmrun/signals_osdep.h 2014-10-05 15:22:24.000000000 +0900
@@ -163,14 +172,24 @@
#elif defined(TARGET_i386) && defined(SYS_bsd_elf)
- #define DECLARE_SIGNAL_HANDLER(name) \
- static void name(int sig, siginfo_t * info, struct sigcontext * context)
+ #if defined (__NetBSD__)
+ #include <ucontext.h>
@pocarist
pocarist / hindleyMilnerTypeInference.fs
Last active June 27, 2023 18:25
Hindley-Milner型推論アルゴリズムをF#で書いてみた - http://d.hatena.ne.jp/pocarist/20141220/1419087653
(* Groovy:
/**
* Hindley-Milner type inference
* Ported to Groovy from Scala by Example Chapter 16 code.
* (http://www.scala-lang.org/docu/files/ScalaByExample.pdf)
* refered also
* https://github.com/namin/spots/blob/master/hindleyMilner/typeInference.scala
* Japanese page of 'Scala by Example':
* http://www29.atwiki.jp/tmiya/pages/78.html
*/ *)