Skip to content

Instantly share code, notes, and snippets.

View Sophia-Gold's full-sized avatar

Sophia Gold Sophia-Gold

View GitHub Profile
@Sophia-Gold
Sophia-Gold / PowSL.hs
Created April 16, 2018 04:29
Power Serious using -XOverloadedLists
{-# LANGUAGE OverloadedLists #-}
import GHC.Exts
newtype PowS' a = PowS' { fromPowS :: [a] }
instance IsList (PowS' a) where
type (Item (PowS' a)) = a
fromList a = PowS' a
toList a = fromPowS a
@Sophia-Gold
Sophia-Gold / FixT.hs
Last active April 13, 2018 01:30
type level `fix` for calculating supernecklace cardinality
{-# LANGUAGE DeriveFoldable #-}
import Data.List
data NList a = [a] :-: (NList [a])
| Nil deriving (Show, Foldable)
fixT :: NList a -> NList a
fixT Nil = Nil
fixT (x :-: Nil) = x :-: (subsequences x :-: Nil)
@Sophia-Gold
Sophia-Gold / Iterate.hs
Created April 6, 2018 09:56
type level iterate
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE KindSignatures #-}
data Nat = Z | S Nat
type family Iterate (n :: Nat) f a where
Iterate Z f a = a
Iterate (S n) f a = f (Iterate n f a)
@Sophia-Gold
Sophia-Gold / Montgomery.hs
Created April 4, 2018 07:11
Montgomery multiplication in Haskell
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE OverlappingInstances #-}
module Montgomery where
-- type R = 2
r :: Integer
r = 2
@Sophia-Gold
Sophia-Gold / ones-in-partitions.clj
Created March 28, 2018 05:56
two ways to count the number of 1s in all partitions of a given integer
(defn ones-in-partitions
"Number of 1s in all partitions of an integer n"
[n]
(-> (repeat (dec n) 1)
(clojure.math.combinatorics/partitions)
(count)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defn partitions
@Sophia-Gold
Sophia-Gold / LOC.sh
Last active July 30, 2019 08:43
count LOC with git
#!/bin/bash
git diff --stat 4b825dc642cb6eb9a060e54bf8d69288fbee4904
@Sophia-Gold
Sophia-Gold / dlist.clj
Created March 17, 2018 03:29
Difference Lists in Clojure
(defn dl
[& elements]
(fn [x] (concat elements x)))
(defn dl-un
[l]
(l nil))
(defn dl-concat
[& lists]
(fn [x] ((apply comp lists) x)))
@Sophia-Gold
Sophia-Gold / quine.scm
Created March 1, 2018 08:00
canonical Scheme quine
((lambda (x) (list x (list 'quote x)))
'(lambda (x) (list x (list 'quote x))))
(defmacro loop-while
[bind condition & body]
`(loop ~bind
(when ~condition
~@body
(recur ~@(map second (partition 2 bind))))))
@Sophia-Gold
Sophia-Gold / inspect-thread.clj
Created February 12, 2018 00:06
debugging clojure thread macros
;; Version 1: direct inline the anonymous function:
(defn as-currency
"Money amounts are transmitted as \"$2.44\".
Parse this and return a numeric type."
[currency-amount]
(-> (subs currency-amount 1)
((fn [x] (println x) x))
(bigdec)
((fn [x] (println x) x))))