This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type Prim = | |
| | Add | |
| | Sub | |
| | Mul | |
| | Div | |
| | Eq | |
| | Not | |
| type Value = | |
| | Bool of bool |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @parser::members { | |
| private bool CanInsertSemicolon { | |
| get { | |
| var i = input.LA(1); | |
| return (i == RCURL | |
| || i == EOF | |
| || input.LT(1).Line > input.LT(-1).Line); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| % ``\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} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type R = | |
| { | |
| A : int | |
| B : int | |
| C : int | |
| } | |
| let Test = | |
| { | |
| A = (printf "A"; 1) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function merge(a, b) { | |
| if (typeof b == "object") { | |
| for (var p in b) { | |
| a[p] = b[p]; | |
| } | |
| return a; | |
| } else { | |
| return b; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /// 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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (* 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. *) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# 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 |