This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{- | |
FirstScript.hs | |
Akhilesh . S | |
-} | |
-- The Value size is an integer (Int), defined to be | |
-- the sum of twelve and thirteen. | |
fac :: Int -> Int |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
user=> (defn prime? [x] | |
#_=> (empty? (filter #(= 0 (rem x %)) (range 2 (inc (int (Math/sqrt x))))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
user=> (def ! #(reduce * (take % (iterate inc 1)))) | |
#'user/! | |
user=> (! 5) | |
120 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template<class A> | |
class List { | |
Node *head; | |
public: | |
List(): head(nullptr) {} | |
List(A a, List<A> l) | |
: head(new Node(a, l)) | |
{} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |