Skip to content

Instantly share code, notes, and snippets.

View ramntry's full-sized avatar

Roman Tereshin ramntry

View GitHub Profile
Require Import Basics.
Require Import BinInt.
Require Import Zbool.
Require Import List.
Require Import ssreflect.
Open Scope Z_scope.
Open Scope list_scope.
@ramntry
ramntry / QuadraticEquationSolver.scala
Created September 22, 2014 13:53
Type inference + Function overloading
object QuadraticEquationSolver {
def solve(a: Double, b: Double, c: Double) = {
val discriminant = b*b - 4*a*c
if (discriminant < 0)
List()
else {
val sqrtOfDisr = math.sqrt(discriminant)
List((-b - sqrtOfDisr)/2, (-b + sqrtOfDisr)/2)
}
}
@ramntry
ramntry / hw02misc_task02.hs
Last active August 29, 2015 14:06
Functional Programming
import Control.Monad.State
import qualified Data.Map as Map
-- Мини-фреймворк для автоматической мемоизации рекурсивных функций.
-- Построен с помощью монады State.
-- Чтобы воспользоваться фреймворком, нужно описать требуемое вычисление
-- в монадической форме и с открытой рекурсией. Последнее означает,
-- что пользовательская функция должна в качестве первого дополнительного
@ramntry
ramntry / automemoization.hs
Last active September 25, 2022 13:05
Auto-memoization in Haskell by State monad
import Control.Monad.State
import qualified Data.Map as Map
recall :: Ord a => a -> State (Map.Map a r) (Maybe r)
recall n = do
memory <- get
return (Map.lookup n memory)
memorize :: Ord a => a -> r -> State (Map.Map a r) ()
memorize n result = do
@ramntry
ramntry / sema.hs
Last active August 29, 2015 14:06
Language L with post-increment and post-decrement expressions
import Control.Exception.Base (assert)
import Data.Function (on)
import Data.List (intercalate)
import qualified Data.Map as Map
type ErrorHandler a = String -> a
languageLError message = error ("[Language L] " ++ message ++ ".")
internalError message = languageLError ("Internal Error: " ++ message)
expressionError message = languageLError ("Expression Evaluation: " ++ message)
@ramntry
ramntry / gadt_map.ml
Last active August 29, 2015 14:06
Extended GADT example in OCaml
(* ('a, 'v) expr GADT:
* 'a stands for type of expression constants.
* 'v stands for type of result of expression evaluation.
*
* For most cases an expression evaluates to value of the same
* type as it's constants, but if the last operator of expression
* is a comparison (Less constructor) or negation (Not constructor)
* the whole expression has a boolean type. Note that in that case
* 'a parameter stays the same, probably non-boolean type. What actually
* changes is 'v. So, the following statement is true:
module Make_expr (Args :
sig
type a
end)
=
struct
include Args
type _ t =
| Const : a -> a t
@ramntry
ramntry / gadt.ml
Created September 16, 2014 20:33
OCaml GADT's usage example
type _ t =
| Const : int -> int t
| Add : int t * int t -> int t
| Less : int t * int t -> bool t
| IfThenElse : bool t * int t * int t -> int t
let rec eval : type a. a t -> a = function
| Const x -> x
| Add (l, r) -> (eval l) + (eval r)
| Less (l, r) -> (eval l) < (eval r)
@ramntry
ramntry / Makefile
Last active August 29, 2015 14:06
Example of transformable irregular data type in OCaml
run_tests: irregular_tree.mli irregular_tree.ml run_tests.ml
ocamlopt irregular_tree.mli irregular_tree.ml run_tests.ml -o $@
#include <cassert>
#include <functional>
std::function<int()> create_counter(int const delta) {
int counter = 0;
return [delta, counter]() mutable {
return counter += delta;
};
}