Skip to content

Instantly share code, notes, and snippets.

View ppsdatta's full-sized avatar

Sourav Datta ppsdatta

View GitHub Profile
@ppsdatta
ppsdatta / date-stream.lisp
Last active June 26, 2019 16:43
A simple function to generate streams of dates based on certain params (uses simple-date-time Common Lisp library)
(defun date-stream (&key (start-date nil) (interval 1) (interval-type :day))
(let ((start (if (not start-date) (now) start-date)))
(lambda ()
(let ((current start))
(setq start (funcall (cond
((eq interval-type :day) #'day+)
((eq interval-type :month) #'month+)
((eq interval-type :year) #'year+)
(t #'day+)) start interval))
current))))
@ppsdatta
ppsdatta / clojure-code-book.md
Last active October 4, 2021 13:39
A collection of useful Clojure functions

Read next value from stdin

(defn next-seq
  [& {:keys [sep type] :or {sep " " type nil}}]
  (let [line (read-line)
        parts (clojure.string/split line (re-pattern sep))]
    (case type
      :int (map #(Integer/parseInt %) parts)
 :double (map #(Double/parseDouble %) parts)
@ppsdatta
ppsdatta / server.md
Last active April 14, 2021 18:00
A simple clack server with Hunchentoot backend and routing via myway

Requirements

  • clack
  • myway
  • quicklisp

Code

(defpackage :server
@ppsdatta
ppsdatta / huffman.rkt
Last active April 27, 2021 09:27
Huffman encoding and decoding in Racket - sample code
#lang racket
(define (freq-list str)
(let ([sl (map (λ (x) (format "~a" x))
(string->list str))])
(hash->list
(make-hash
(map (λ (x) (cons (list x)
(count (λ (y) (string=? x y)) sl)))
@ppsdatta
ppsdatta / queens.lisp
Created May 1, 2021 07:51
Brute force N queens problem solver - SBCL gives up after 7x7 board
(defun all-pos (n)
(loop for i from 0 to (- n 1)
append (loop for j from 0 to (- n 1)
collect (cons i j))))
(defun single-queen (n)
(loop for i in (all-pos n)
collect (list i)))
(defun safep (p moves)
@ppsdatta
ppsdatta / queens.rkt
Last active May 4, 2021 05:56
8 Queens Problem in Racket
#lang racket
(define (all-positions n)
(for*/list ([i n]
[j n])
(cons i j)))
(define (single-solutions n)
(for/list ([i (all-positions n)])
@ppsdatta
ppsdatta / sqaureroot.sb
Created May 5, 2021 11:04
Newton's square root method in Small Basic
' Newton's square root method
TextWindow.Write("Enter a positive number: ")
n = TextWindow.ReadNumber()
guess = 1.0
Calculate:
diff = Math.Abs(n - (guess * guess))
If diff <= 0.001 Then
@ppsdatta
ppsdatta / sha256demo.c
Last active June 18, 2021 13:48
A SHA256 hash generator in C using openssl
#include <openssl/hmac.h>
#include <openssl/sha.h>
#include <openssl/evp.h>
#include <string.h>
#define EXPECTED "0d696548764b6f910bdd3c07d8e465112c0783a03f2c1cf5ef893b8aa27f3290"
int main()
{
unsigned char *message = (unsigned char *)"this is highly sensitive user data";
@ppsdatta
ppsdatta / hmac.lisp
Created June 20, 2021 19:02
Generate hmac with sha256 in Common Lisp
(ql:quickload :ironclad)
(defpackage :test (:use :cl :ironclad))
(in-package :test)
(defun hmac-sha256 (secret data)
(let* ((secret-bytes (ascii-string-to-byte-array secret))
(data-bytes (ascii-string-to-byte-array data))
(h (make-hmac secret-bytes :sha256)))
(update-hmac h data-bytes)
(byte-array-to-hex-string (hmac-digest h))))
@ppsdatta
ppsdatta / cat.pas
Created July 7, 2021 14:53
A simple program to display files in paginated manner in Turbo Pascal 3 on CP/M 2
program cat(input, output);
var
fname: string[60];
f: text;
ln: string[100];
rw: integer;
i: integer;
begin
write('FILE? ');
readln(fname);