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
;;;; cf. http://riktor.hatenablog.com/entry/2012/08/14/235214 | |
'(1 (+ 2 3) (+ 4 5)) | |
`(1 ~(+ 2 3) (+ 4 5)) | |
`(1 ~@(list 2 3 4)) | |
`(1 ~@'(2 3 4)) | |
`(1 ~@(map (fn [x] (* x 2)) '(1 2 3)) 5) | |
(defn my-append [coll1 coll2] | |
`(~@coll1 ~@coll2)) |
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
module WriterTest where | |
import Control.Monad.Writer (Writer, runWriter, tell) | |
gcd' :: Int -> Int -> Writer [String] Int | |
gcd' a b | |
| b == 0 = do | |
tell ["Finished with " ++ show a] | |
return a | |
| otherwise = do |
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 my-odd? [n] | |
(letfn [(odd?? [n] | |
(if (zero? n) | |
false | |
#(even?? (dec n)))) | |
(even?? [n] | |
(if (zero? n) | |
true | |
#(odd?? (dec n))))] | |
(trampoline odd?? n))) |
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 pow-cps-trampoline [n m] | |
(letfn [(pow [n m f] | |
(if (zero? m) | |
(f 1N) | |
(fn [] (pow n (dec m) (fn [x] #(f (* n x)))))))] | |
(trampoline pow n m identity))) | |
(do (println (pow-cps-trampoline 2 10)) | |
(println (pow-cps-trampoline 2 100)) | |
(println (pow-cps-trampoline 2 1000)) |
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 sumProcedural(m: Int): Int = { | |
var sum = 0 | |
for (n <- 1 to m) { | |
sum += n | |
} | |
sum | |
} | |
def sumProcedural2(m: Int): Int = { | |
var sum = 0 |
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
class Rational(n: Int, d: Int) { | |
init { | |
require(d != 0, {"denominator must not be null"}) | |
} | |
private val g by lazy { gcd(Math.abs(n), Math.abs(d)) } | |
val numerator: Int by lazy { n / g } | |
val denominator: Int by lazy { d / g } | |
operator fun plus(that: Rational): Rational = | |
Rational( | |
numerator * that.denominator + that.numerator * denominator, |
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 ・8・ [] | |
(->> '(・8・) | |
(repeat (inc (rand-int 5))) | |
(apply str))) | |
(println (・8・)) |
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
@ case class UserAge(id: Int, age: Int) | |
defined class UserAge | |
@ val userAges = Map(1 -> UserAge(1, 25), 2 -> UserAge(2, 15), 3 -> UserAge(3, 35), 4 -> UserAge(4, 5), 5 -> UserAge(5, 45)) | |
userAges: Map[Int, UserAge] = Map(5 -> UserAge(5, 45), 1 -> UserAge(1, 25), 2 -> UserAge(2, 15), 3 -> UserAge(3, 35), 4 -> UserAge(4, 5)) | |
// SELECT user_age.age | |
// FROM user_age | |
// WHERE user_age.age >= 20; | |
@ userAges.filter { case (_, ua) => ua.age >= 20 }.mapValues(_.age) | |
res2: Map[Int, Int] = Map(5 -> 45, 1 -> 25, 3 -> 35) |
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
(require '[clojure.set :refer [union]]) | |
;; S_0 | |
(def s0 #{}) | |
;; S_1 | |
(def s1 (union #{true false 0} | |
(for [t s0] ['succ t]) | |
(for [t s0] ['pred t]) | |
(for [t s0] ['iszero t]) |