Skip to content

Instantly share code, notes, and snippets.

@GeoffChurch
GeoffChurch / n-byte-wise_seq2seq.py
Last active February 21, 2016 04:36 — forked from karpathy/min-char-rnn.py
Minimal character-level language model with a Vanilla Recurrent Neural Network, in Python/numpy
"""
Minimal character-level Vanilla RNN model. Written by Andrej Karpathy (@karpathy). Edited by Geoffrey Churchill
BSD License
"""
import numpy as np
from sys import argv
try:
num_steps=int(argv[3])
except IndexError:
@GeoffChurch
GeoffChurch / .emacs
Last active May 2, 2017 20:16
.emacs
(menu-bar-mode -1)
(setq inhibit-startup-screen t)
(setq inhibit-startup-echo-area-message "l")
(setq initial-major-mode 'fundamental-mode)
(setq initial-scratch-message nil)
(setq auto-save-default nil)
(setq make-backup-files nil)
(require 'package)
;;(add-to-list 'package-archives
@GeoffChurch
GeoffChurch / settings.json
Last active August 7, 2021 00:48
VS Code settings
{
"editor.acceptSuggestionOnEnter": "off",
"editor.selectionClipboard": false, // Disable middle-click paste.
"telemetry.enableTelemetry": false,
"telemetry.enableCrashReporter": false,
"workbench.startupEditor": "none",
"window.zoomLevel": 3,
"window.restoreFullscreen": true,
"workbench.statusBar.visible": true,
"workbench.activityBar.visible": false,
@GeoffChurch
GeoffChurch / zip.hs
Last active December 15, 2020 14:40
zip in terms of foldr
import Prelude hiding (zip)
-- `zip` accepts the first list and lazily returns a
-- function that accepts the second list and lazily
-- returns the list of pairs, so this implementation
-- is lazy in both arguments.
zip :: [a] -> [b] -> [(a, b)]
zip =
foldr
zipxs
@GeoffChurch
GeoffChurch / diagonalApply.hs
Last active March 7, 2021 16:28
"Diagonalized" version of <*> for lists, which reaches every finitely reachable application
ap :: [a -> b] -> [a] -> [b]
ap fs xs = go1 ([], fs) ([], xs)
where
go1 (fs, []) (_, xs) = [f x | x <- xs, f <- fs] -- No more fs: roundrobin seen fs on unseen xs.
go1 fs@(_, f : _) xs@(seenxs, _) = [f x | x <- seenxs] ++ go2 (step fs) xs -- Apply next f to seen xs and recur.
go2 (_, fs) (xs, []) = [f x | f <- fs, x <- xs] -- No more xs: roundrobin seen xs on unseen fs.
go2 fs@(seenfs, _) xs@(_, x : _) = [f x | f <- seenfs] ++ go1 fs (step xs) -- Apply seen fs to next x and recur.
step (seen, cur : unseen) = (cur : seen, unseen) -- Mark current element as seen.
-- E.g. to generate all triples
@GeoffChurch
GeoffChurch / binaryRelations.hs
Created March 30, 2021 18:44
Lazy subsets, product, and binary relations between potentially infinite lists
-- Lazy subsets of potentially infinite list (from https://stackoverflow.com/a/36101787).
subsets :: [a] -> [[a]]
subsets l = [] : go [[]] l
where
go _ [] = []
go seen (cur : rest) = let next = (cur :) <$> seen in next ++ go (seen ++ next) rest
-- Lazy product of potentially infinite lists.
product :: [a] -> [b] -> [(a, b)]
product l r = go1 ([], l) ([], r)
@GeoffChurch
GeoffChurch / array.pl
Created August 22, 2021 04:10
Multidimensional arrays in Prolog
:- use_module(library(clpfd)).
%% at(Dimensions, Index, Array, Element) -- array access.
at([], [], X, X).
at([D|Ds], [I|Is], A, X) :-
length(A, D),
nth1(I, A, AI),
at(Ds, Is, AI, X).
%% `Ds` is the size of each dimension of an array. `Is` is the list of all indices into an array with those dimensions.
@GeoffChurch
GeoffChurch / lambda.pl
Last active December 14, 2021 00:58
Implementation of call-by-name lambda calculus in Prolog using logic variables as lambda variables
% Implementation of call-by-name lambda calculus in Prolog using logic variables as lambda variables
%
% The grammar is:
% Term ::= Term-Term % abstraction: LHS is function body; RHS is parameter (Y-X instead of λX.Y)
% | Term+Term % application: LHS is function; RHS is argument (F+X instead of (F X))
eval(Y-X, Y-X). % abstractions are left as-is
eval(F+X, Y) :-
copy_term(F,B), % copy before destructive unification of parameter in case F appears elsewhere
eval(B, Y0-X), % eval into what must be an abstraction and unify X with parameter
@GeoffChurch
GeoffChurch / coalesce_variables.pl
Last active May 20, 2022 17:54
coalesce_variables/1 applies an equivalence relation to the variables of a term.
eos([], []).
% Relates a list to a partitioning of its elements into two subsequences (append/3 for subsequences).
list_subseq_subseq([]) --> eos.
list_subseq_subseq([H|S]) --> [H], list_subseq_subseq(S).
list_subseq_subseq([H|S]), [H] --> list_subseq_subseq(S).
% Relates a list to a partitioning of its elements into nonempty subsequences (append/2 for nonempty subsequences).
list_subseqs([], []).
list_subseqs([H|T], [[H|PT]|Ps]) :-
@GeoffChurch
GeoffChurch / string_rewriting.pl
Last active December 14, 2021 14:46
String rewriting (SRS) in Prolog using DCGs
rewrite(L, R), [Skip] --> [Skip], rewrite(L, R). % Skip the current head.
rewrite(L, R), R --> L. % Match the current prefix and return.
rewrite(Rules) --> {member(L-R, Rules)}, rewrite(L, R). % Try a single rule.
normalize(P) --> P, normalize(P). % If at first you succeed, try, try again.
normalize(P) --> \+P. % P failed so list remains the same.
% Single-step rewrite with {"ab"->"x", "ca"->"y"}:
% ?- phrase(rewrite([[a,b]-[x], [c,a]-[y]]), [a,b,c,a,b,c], Out).
% Out = [a, b, c, x, c] ;