Skip to content

Instantly share code, notes, and snippets.

(declare lookup [sym env])
(defn lookup [sym [sym val & _]] val)
(defn lookup [sym [_ _ & rest]] (lookup sym rest))
(defn lookup [sym []] (error "Symbol " syn " not found"))
(declare eval [expr env])
(defn eval [expr _] (error "Invalid expression" expr))
(defn eval [(int n) env] n)
(defn eval [(float n) env] n)
(defpred + [t]
(+ [t t] t))
(deferr + [t]
"Type " t " does not have an addition operation defined on it.")
(defrule + [int]
(+ [v1 v2] (int-+ v1 v2)))
(defrule + [float]
(+ [v1 v2] (float-+ v1 v2)))
(defpred lookup [env sym > t]
(defpred forte [type]
(forte-val [type] int))
(defrule (forte ())
(forte-val [_] 0))
(defrule (forte ('O & rest))
[(forte rest)]
(forte-val [(_ & rest)]
(* (forte-val rest) 2)))
type result(T, E) = ok(T) | err(E).
decl E is error, T1 is any, T2 is any, F is T1 -> result(T2, E) =>
check(result(T1, E), F) -> result(T2, E).
check(R, F) := case R of {
ok(V) => F!V,
err(E) => err(E)
}.
@brosenan
brosenan / main.dart
Created August 2, 2019 10:34
A "Game of Sorts"
import 'package:flutter/material.dart';
import 'dart:math';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
use std::collections::HashMap;
use std::sync::Arc;
#[derive(Clone)]
pub enum Value {
Num(f64),
Str(String),
Struct(HashMap<String, Arc<Value>>),
}
use std::ops::{Mul, Add};
struct Parser {
parser: Box<Fn(&[u8], usize) -> Vec<usize>>,
}
fn empty() -> Parser {
Parser{parser: Box::new(|_s: &[u8], idx| vec![idx])}
}
#[derive(Debug)]
struct List<T> {
elem: T,
next: Option<Box<List<T>>>,
}
#[derive(Debug)]
struct Queue<T> {
tail: Option<Box<List<T>>>,
head: *mut Option<Box<List<T>>>,
use std::collections::HashMap;
use std::hash::Hash;
struct Memoize<F, T1, T2> where F: Fn(&T1) -> T2 {
cache: HashMap<T1, T2>,
func: F,
}
impl<F, T1, T2> Memoize<F, T1, T2> where F: Fn(&T1) -> T2, T1: Eq + Hash, T2: Clone {
fn new(func: F) -> Memoize<F, T1, T2> {
@brosenan
brosenan / linear-types.clj
Created October 19, 2018 05:59
Example code in an imaginary, linearly-typed functional programming language.
;; This is not really Clojure code. I used the .clj extention since it provides the closests syntax highlighting to this imaginary language.
(defabst constantly [?t] (-> ?t (-> ?t))
(fn [c]
(fn []
(copy c))))
(deftype List [?t]
(::nil)
(::item ?t (List ?t)))