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
datatype expr = PLUS of expr * expr | |
| NUM of int | |
fun eval_expr (NUM n) = n | |
| eval_expr (PLUS (l,r)) = eval_expr l + eval_expr r | |
fun pretty_expr (NUM n) = Int.toString n | |
| pretty_expr (PLUS (l,r)) = "( " ^ pretty_expr l ^ " + " ^ pretty_expr r ^ " )" |
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
fun y f x = f (y f) x | |
functor Fix(type 'a f ) | |
= struct | |
type 'a f = 'a f | |
datatype fix = FIX of fix f | |
fun inj v = FIX v | |
fun prj (FIX v) = v | |
end |
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
// // if it was C : | |
// | |
// enum t { | |
// Plus = 0, | |
// Mul = 1, | |
// num = 2, | |
// } | |
// union e { plus : Plus<*const TG>, mul : Mul<*const TG>, num : u32 } | |
// struct TG { tag : int, val : e } |
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
// this is just to satisfy the type system as I don't have the function | |
mod boys { pub mod micb25 { pub fn boys(_:u64,_:f64)->f64 {1.0} }} | |
#[derive(Clone, Copy)] | |
pub struct Vector3<T> {x:T,y:T,z:T} | |
impl Vector3<f64> { | |
fn norm_squared(&self)->f64 {self.x*self.x+self.y*self.y+self.z*self.z} | |
} | |
// direct style |
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
use std::marker::PhantomData; | |
enum Ast{}; | |
enum Eval{}; | |
trait Mode {} | |
impl Mode for Ast {} | |
impl Mode for Eval {} |
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
// original | |
// fn hermite_expansion(i: i32, j: i32, t: i32, dist: f64, a: f64, b: f64) -> f64 { | |
// let p = a + b; | |
// let q = a * b / p; | |
// if t < 0 || t > i + j { | |
// 0.0 | |
// } else if i == j && j == t && t == 0 { | |
// f64::exp(-q * dist.powi(2)) |
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
structure Plus = struct | |
datatype 'a t = T of 'a -> 'a -> 'a | |
end | |
structure Mul = struct | |
datatype 'a t = T of 'a -> 'a -> 'a | |
end | |
structure FromInt = struct |
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
(* direct style *) | |
fun substractorial n = | |
if n < 0 | |
then 0 | |
else n - substractorial (n-1) | |
(* Continuation passing style *) | |
(* In a functional language, when every branch is a "tail call", it optimizes to a jumps *) | |
(* this could already be turned into a loop pretty easily, |
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
const interface_method = (object, _interface)=>(method) => ( | |
object._interface[_interface].hasOwnProperty(method) | |
&& (typeof object._interface[_interface][method]) == "function" | |
) | |
const implInterface = (_interface, invariant, object, impl) => { | |
if (!object.hasOwnProperty("_interface")) { | |
object._interface = {}; | |
} | |
object._interface[_interface] = impl | |
if (!invariant(object)) {throw `FAILED TO IMPLEMENT \`${_interface}\``} |
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
#![allow(unused)] | |
use std::{borrow::Borrow, convert::Infallible}; | |
trait Functor { | |
type F<T>; | |
fn fmap<A, B>(f: impl Fn(A) -> B, x: Self::F<A>) -> Self::F<B>; | |
} | |
trait Rec: Sized { | |
type Fix; |