Skip to content

Instantly share code, notes, and snippets.

View lagenorhynque's full-sized avatar
🐬
architecting & managing

Kent OHASHI lagenorhynque

🐬
architecting & managing
View GitHub Profile
;;;; 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))
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
(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)))
(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))
def sumProcedural(m: Int): Int = {
var sum = 0
for (n <- 1 to m) {
sum += n
}
sum
}
def sumProcedural2(m: Int): Int = {
var sum = 0
@lagenorhynque
lagenorhynque / enjoyable-lisp.md
Last active November 11, 2022 07:21
たのしいLisp

たのしいLisp


自己紹介


大橋 賢人 (@lagenorhynque)

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,
(defn ・8・ []
(->> '(・8・)
(repeat (inc (rand-int 5)))
(apply str)))
(println (・8・))
@ 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)
(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])