Skip to content

Instantly share code, notes, and snippets.

@souravdatta
souravdatta / tasks.rkt
Last active January 2, 2024 15:35
Tasks with local database implemented in Racket
#lang racket
(require db)
(require racket/date)
(define (connect file)
(sqlite3-connect #:database "/Users/sdatta/code/tasks.sqlite3"))
(define (add-task! conn
@souravdatta
souravdatta / genes.p
Created January 1, 2024 16:43
Genetic algo
program Genes;
uses crt, SysUtils;
const
CODONS = 'abcdefghijklmnopqr stuvwxyz0123456789';
TARGET = 'happy new year 2024';
PopulationSize = 1000;
type
StringArray = array[1..PopulationSize] of string;
@souravdatta
souravdatta / algos.rkt
Created December 26, 2023 10:33
Some common algorithms in Racket
#lang racket
(define (nest-list f exp n)
(do ([i 0 (+ i 1)]
[x exp (f x)]
[a '() (cons x a)])
((>= i n) a)))
(define (nest-while f exp cnd)
@souravdatta
souravdatta / algorithms.lisp
Last active December 26, 2023 10:04
Algorithms in Common Lisp
(defun nest-list (f exp n)
(do ((i n (- i 1))
(n exp (funcall f n))
(x nil (cons n x)))
((< i 0) x)))
(defun nest-while (f1 exp f2)
(do ((x exp (funcall f1 x))
(a nil (cons x a)))
((not (funcall f2 x)) a)))
@souravdatta
souravdatta / weasel.clj
Last active June 19, 2023 05:05
Weasel program in Clojure
(def codons (conj (map char (range (int \A) (inc (int \Z)))) \space))
(defn rand-char []
(rand-nth codons))
(defn rand-str [len]
(apply str (map (fn [_] (rand-char))
(range len))))
(defn in-range? [n r]
@souravdatta
souravdatta / perceptrons.rkt
Created April 8, 2023 07:00
Functional Perceptrons
#lang racket
(require plot)
(define (sigmoid x)
(/ 1.0 (+ 1 (exp (- x)))))
(define (step x)
(if (>= x 0) 1 0))
@souravdatta
souravdatta / perceptron.fs
Created April 6, 2023 12:22
A simple perceptron in F#
open System
let hello (name: string) : string = "Hello " + name
let dotProduct (xs: double array) (ys: double array) : double =
Array.zip xs ys |> Array.map (fun (x, y) -> x * y) |> Array.sum
let activate (w: double) : double = if w >= 0.0 then 1.0 else 0.0
let predict (ws: double array) (inputs: double array) : double = dotProduct ws inputs |> activate
@souravdatta
souravdatta / seq.lisp
Created February 19, 2023 07:27
A small genetic sequence library inspired by Clojure
(defclass seq () ())
(defmethod s-first ((s seq))
"The first element, if exists"
(error "Implement in child class"))
(defmethod s-rest ((s seq))
"The rest of the sequence, if exists"
(error "Implement in child class"))
@souravdatta
souravdatta / adv11.rb
Created December 11, 2022 17:16
Brute force AoC 11
class Monkey
attr_accessor :items, :inspect_count
def initialize(game, name, items, expr, test, tmonkey, fmonkey)
@name = name
@items = items
@expr = expr
@test = test
@tmonkey = tmonkey
@fmonkey = fmonkey
@souravdatta
souravdatta / code.apl
Last active August 2, 2022 17:53
Some interesting APL code fragments
Deltas ← {¯1×(-/)¨2,/0,⍵}
Sells ← 2 4 8 10 12 24 25
Buy ← 26
Deltas Buy⌊+\ Sells