Skip to content

Instantly share code, notes, and snippets.

type Prim =
| Add
| Sub
| Mul
| Div
| Eq
| Not
type Value =
| Bool of bool
import Data.List
primes :: [Int]
primes = 2 : 3 : 5 : filter isPrime [7, 9 ..]
suspects :: Int -> [Int]
suspects n = takeWhile (\x -> x * x <= n) primes
isPrime :: Int -> Bool
isPrime n = all (\x -> n `mod` x /= 0) (suspects n)
@parser::members {
private bool CanInsertSemicolon {
get {
var i = input.LA(1);
return (i == RCURL
|| i == EOF
|| input.LT(1).Line > input.LT(-1).Line);
}
}
@t0yv0
t0yv0 / Listing.tex
Created April 28, 2011 18:14
LaTeX command for typesetting pretty-printed code blocks.
% ``\Listing{...}'' command typesets its argument as a code block -
% with monospace font and whitespace preserved. It is an
% environment-like command accepting multiple paragraphs.
\def\Listing{\bigskip\begingroup%
\setlength{\parskip}{0pc}\addtolength{\parindent}{1pc}%
\tt\scriptsize\obeylines\obeyspaces\PreserveWhitespace\ListingBody}
% ``\ListingBody'' is an auxillary command need to make Listing work.
% It inserts the argument and closes the group opened by Listing.
\long\def\ListingBody#1{#1\endgroup\smallskip}
type R =
{
A : int
B : int
C : int
}
let Test =
{
A = (printf "A"; 1)
@t0yv0
t0yv0 / ClosureSerialization.hs
Created May 31, 2011 13:20
An attempt to figure out an easy way to serialize function types.
{-# OPTIONS -XExistentialQuantification -XMultiParamTypeClasses #-}
-- Serialization requires being able to marshall and unmarshall.
class Serializable a where
serialize :: a -> String
deserialize :: String -> (a, String)
-- Static assertion that `a` encodes `b -> c`.
class Encoding a b c where
decode :: a -> b -> c
function merge(a, b) {
if (typeof b == "object") {
for (var p in b) {
a[p] = b[p];
}
return a;
} else {
return b;
}
}
@t0yv0
t0yv0 / FSCGI.Structural.Converter.fs
Created June 30, 2011 01:07
An attempt to improve on OWIN<http://owin.org> in F#.
/// Converts structural encodings to proper FSCGI.* types.
module FSCGI.Structural.Converter
type private Encodings<'R,'W> =
('W -> Writer<'W>) *
('R -> Response<'R,'W>)
let private ConvertRequest (r : FSCGI.Request) : Request =
(
r.Headers,
@t0yv0
t0yv0 / MonadExamples.fs
Created July 2, 2011 23:29
Emulating higher kinds with F# inline functions.
(* F# lacks proper support for higher-kinded abstraction such as ML
functors or Haskell typeclasses. In particular it makes it seemingly
impossible to define functions that work for every type constructor.
The code below demonstrates that this functionality can be partially
recovered by using inline functions with static member constraints.
The approach requires explicitly passing typeclass instance values.
Error messages and derived types are horrible. Nonetheless, there is
some type safety - if the typeclass instances have been defined right,
type-unsafe code will be rejected by the typechecker, albeit with a
hairy message. Another disadvantage is the need to invent operators. *)
@t0yv0
t0yv0 / Regex.hs
Created July 14, 2011 13:10
A simple regex writeup in Haskell.
{-# OPTIONS -XGADTs #-}
-- This is an excercise in constructing a combinator-based regular
-- expression matcher for self-education.
--
-- Special thanks to Roman Cheplyaka (@shebang) for pointing out to me
-- the need for state reduction to eliminate exponential complexity on
-- certain benchmarks.
module Regex (Regex, SM, token, compile, run, char, string) where