This file contains hidden or 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
quicksort :: (Ord a) => [a] -> [a] | |
quicksort [] = [] | |
quicksort (x:xs) = | |
let small = quicksort [ a | a <- xs, a <=x] | |
large = quicksort [ a | a <- xs, a > x] | |
in small ++ [x] ++ large |
This file contains hidden or 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
;; CLOJURE STM USING REFS | |
;; this is just an immutable declaration | |
(def some-numbers #{1 2 3}) | |
;;if you want to make the above mutable, you can use "refs" | |
(def some-numbers (ref #{1 2 3 })) | |
;; @ is used to dereference data | |
(def current-some-numbers @some-numbers) |
This file contains hidden or 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
;; http://grimoire.arrdem.com/ | |
;;shows how to define lambda expressions in clojure | |
;;one form is a shortcut | |
(defn empty? [string] | |
(every? #(Character/isWhitespace %) string)) | |
;; this one is a little more explicit | |
(defn empty2? [string] | |
(every? (fn [x] (Character/isWhitespace x)) string)) |
This file contains hidden or 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 factorial(num : Int) : Int = { | |
if(num > 1) num * factorial (num-1) else num | |
} | |
factorial(5) ///will print 120 |
This file contains hidden or 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 factorial(number: Int) : Int = { | |
@tailrec def internalFactorial(seed: Int, num : Int) : Int = if (num > 1) internalFactorial (seed * num, num - 1) else num | |
internalFactorial(1, number) | |
} |
This file contains hidden or 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
(defn count-down [n] | |
(do | |
(println "Method Started") | |
(println "Number : " n) | |
(when (pos? n) (recur (dec n))))) | |
;; when you run (count-down 5), output is | |
;; Method Started | |
;; Number : 5 | |
;; Method Started |
This file contains hidden or 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
(defn factorial [number] | |
(loop [num (biginteger number) | |
acc 1] | |
(if (zero? num) acc | |
(recur (dec num) (* acc num))))) | |
;; factorial can now be called on any large numbers |
This file contains hidden or 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
;; Positional Destructuring | |
(def fullname ["Krishna", "Bhargava", "Vangapandu"]) | |
(defn firstName [fullname] | |
; this is positional destructuring - works only on vectors as they are aligned sequentially | |
(let [[fName _ _] fullname] | |
fName)) | |
(defn first5 | |
"returns a vector with the first item as count of the sequence passed. The rest of the items are the first 5 items in the sequence." |
This file contains hidden or 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
(defn are-consecutive? [input] | |
(let [sorted (sort input) | |
diff (map - (rest sorted) sorted) | |
all-one? (every? #(= 1 %) diff)] | |
all-one?)) |
This file contains hidden or 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
(defn seq-merge [a-seq b-seq] | |
(cond (empty? a-seq) b-seq ;; retur non-empty sequence if any of the sequence is empty | |
(empty? b-seq) a-seq | |
:else (let [first-a (first a-seq) | |
first-b (first b-seq)] | |
(if (< first-a first-b) ;;if picking the first item, the rest of a-seq or vice versa | |
(cons first-a | |
(seq-merge (rest a-seq) b-seq)) | |
(cons first-b | |
(seq-merge a-seq (rest b-seq))))))) |