Skip to content

Instantly share code, notes, and snippets.

View akhileshs's full-sized avatar

Akhilesh Srikanth akhileshs

  • San Francisco, CA
View GitHub Profile
@akhileshs
akhileshs / trial1.hs
Created October 7, 2015 18:48
Haskell Trial
{-
FirstScript.hs
Akhilesh . S
-}
-- The Value size is an integer (Int), defined to be
-- the sum of twelve and thirteen.
fac :: Int -> Int
user=> (defn prime? [x]
#_=> (empty? (filter #(= 0 (rem x %)) (range 2 (inc (int (Math/sqrt x)))))))
>>> def prove(f):
... s = Solver()
... s.add(Not(f))
... if s.check() == unsat:
... print "proved"
... else:
... print "failed to prove"
>>> p, q = Bools('p q')
>>> demorgan = And(p, q) == Not(Or(Not(p), Not(q)))
>>> prove(demorgan)
user=> (def ! #(reduce * (take % (iterate inc 1))))
#'user/!
user=> (! 5)
120
user=> (defrecord TreeNode [val l r])
user.TreeNode
user=> (TreeNode. 5 nil nil)
#user.TreeNode{:val 5, :l nil, :r nil}
user=> (defn xconj [t v]
#_=> (cond
#_=> (nil? t) (TreeNode. v nil nil)
#_=> (< v (:val t)) (TreeNode. (:val t) (xconj (:l t) v) (:r t))
#_=> :else (TreeNode. (:val t) (:l t) (xconj (:r t) v))))
#'user/xconj
prodToSum :: (a, Either b c) -> Either (a, b) (a, c)
prodToSum (x, e) =
case e of
Left y -> Left (x, y)
Right z -> Right (x, z)
sumToProd :: Either (a, b) (a, c) -> (a, Either b c)
sumToProd e =
case e of
Left (x, y) -> (x, Left y)
template<class A>
class List {
Node *head;
public:
List(): head(nullptr) {}
List(A a, List<A> l)
: head(new Node(a, l))
{}
};
template <class A>
class Option {
bool isValid;
A value;
public:
Option() : isValid(false) {}
Option(A v) : isValid(true), value(v) {}
bool isValid() const { return isValid; }
A value() const { return value; }
@akhileshs
akhileshs / gist:2594fdaa879f1aeee23a
Created April 3, 2015 13:15
simulating a knight's move
scala> case class KnightPos(c: Int, r: Int)
defined class KnightPos
scala> :pa
// Entering paste mode (ctrl-D to finish)
case class KnightPos(c: Int, r: Int) {
def move: List[KnightPos] =
for {
KnightPos(c2, r2) <- List(KnightPos(c + 2, r - 1), KnightPos(c + 2, r + 1),
scala> trait Functor[F[_]] { self =>
| def map[A, B](fa: F[A])(f: A => B): F[B]
| // lift f to F and apply on F[A]
| // ...
| }
defined trait Functor