Skip to content

Instantly share code, notes, and snippets.

View Abhiroop's full-sized avatar
:electron:
"Syntactic sugar causes cancer of the semicolon."

Abhiroop Sarkar Abhiroop

:electron:
"Syntactic sugar causes cancer of the semicolon."
View GitHub Profile
fuse (T B t1 x t2) (T B t3 y t4) =
let s = fuse t2 t3
in case s of
(T R s1 z s2) -> (T R (T B t1 x s1) z (T B s2 y t4)) -- consfusing case
(T B s1 z s2) -> balL (T B t1 x (T B s y t4))
delete :: (Ord a) => a -> Tree a -> Tree a
delete x t = makeBlack $ del x t
where makeBlack (T _ a y b) = T B a y b
makeBlack E = E
del :: (Ord a) => a -> Tree a -> Tree a
del x t@(T _ l y r)
| x < y = delL x t
| x > y = delR x t
| otherwise = fuse l r
{-# LANGUAGE GADTs, DataKinds #-}
data Nat = Zero | Succ Nat
data T n a = NodeR (Tree n a) a (Tree (Succ n) a) -- right subtree has height + 1
| NodeL (Tree (Succ n) a) a (Tree n a) -- left subtree has height + 1
| Node (Tree n a) a (Tree n a) -- both subtrees are of equal height
data Tree n a where
Branch :: T n a -> Tree (Succ n) a
Leaf :: Tree Zero a
delL :: (Ord a) => a -> Tree a -> Tree a
delR :: (Ord a) => a -> Tree a -> Tree a
delL x t@(T R t1 y t2) = T R (del x t1) y t2
delR x t@(T R t1 y t2) = T R t1 y (del x t2)
pascalLevel :: [[Int]]
pascalLevel = [1] : map foo pascalLevel
foo :: [Int] -> [Int]
foo x = zipWith (+) ([0] ++ x) (x ++ [0])
-- take 5 pascalLevel
-- [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
-------------------------------------------------------
partitionBy :: Eq b => (a -> b) -> [a] -> [[a]]
partitionBy f [] = []
partitionBy f [x] = [[x]]
partitionBy f l = partitionBy' f (head l) (tail l)
partitionBy' _ x [xs] = [x : [xs]]
partitionBy' f x xs@(x':_)
| (f x) == (f x') = (x : (head generator)) : (tail generator) -- the recursion is delayed here by lazily creating a generator
| otherwise = [x] : generator
where
;; Bear in mind this is a literal translation of the Haskell partitionBy.
;; This might not be the most idiomatic clojure.
(defn partition-by'' [f head tail]
(if (empty? tail)
(cons [head] tail)
(let [generator (partition-by'' f (first tail) (next tail))]
(if (= (f head) (f (first tail)))
(cons (cons head (first generator)) (next generator))
#include <stdio.h>
#include <xmmintrin.h> //SSE
inline __m64 shuf(int perm){
__m64 a = _mm_setr_pi16(1,2,3,4);
return _mm_shuffle_pi16 (a, perm);
}
int main()
{