Skip to content

Instantly share code, notes, and snippets.

@kohyama
kohyama / synhl.scm
Created November 5, 2012 05:40
Syntax Highlighter in Gauche
(define-module synhl
(use text.tree)
(export highlight))
(select-module synhl)
;(use slib)
;(require 'trace)
(define (mk-tokenizer :rest args)
(letrec
@kohyama
kohyama / plexho.clj
Created November 8, 2012 16:30
Hanoi Operations
(defn hanoi-ops [s t d n]
(if (zero? n) '()
(concat
(hanoi s d t (dec n))
(list [n s d])
(hanoi t s d (dec n)))))
(hanoi-ops 'A 'B 'C 4)
; -> ([1 A B] [2 A C] [1 B C] [3 A B] [1 C A] [2 C B] [1 A B] [4 A C]
; [1 B C] [2 B A] [1 C A] [3 B C] [1 A B] [2 A C] [1 B C])
@kohyama
kohyama / README.md
Last active October 13, 2015 19:18
Project Euler Problem 10

Project Euler Problem 10 解答とコメント

#clojure 入門者向け勉強会 #mitori_clj 第二週担当分

Project Euler Problem 10
2,000,000 未満の素数の総和を求めよ. とのことです.

結果を出すために手に入るリソースを有効活用するという考えのもとでは --この問題自体の解答を述べている

@kohyama
kohyama / check_inline.clj
Created December 11, 2012 03:02
Check how things are going about expansion and loading in clojure
(ns check-inline
(:gen-class))
(defmacro sqr-m [n] `(* ~n ~n))
(definline sqr-i [n] `(* ~n ~n))
(defn sqr-fwi
{:inline (fn [n] `(do (println "inline:") (* ~n ~n)))}
[n] (do (println "function:") (* n n)))
@kohyama
kohyama / plex02.clj
Created October 23, 2012 06:01
Excercise 02 Fibonacci Sequence
(defn fibs [a b]
(cons a (lazy-seq (fibs b (+ a b)))))
(take 100 (fibs 1N 1N))
; -> (1N 1N 2N 3N 5N 8N 13N 21N 34N 55N 89N 144N 233N 377N 610N 987N
; 1597N 2584N 4181N 6765N 10946N 17711N 28657N 46368N 75025N
; 121393N 196418N 317811N 514229N 832040N 1346269N 2178309N
; 3524578N 5702887N 9227465N 14930352N 24157817N 39088169N
; 63245986N 102334155N 165580141N 267914296N 433494437N
; 701408733N 1134903170N 1836311903N 2971215073N 4807526976N
(ns barnsleys-fern)
(defn- affine [a b]
(fn [x] (map #(apply + (conj (map * %1 x) %2)) a b)))
(defn- move [x]
(let [afs (mapv (partial apply affine)
[[[[ 0.0 0.0 ] [ 0.0 0.16]] [0.0 0.0 ]]
[[[ 0.85 0.04] [-0.04 0.85]] [0.0 1.6 ]]
[[[ 0.2 -0.26] [ 0.23 0.22]] [0.0 1.6 ]]
@kohyama
kohyama / pep017.clj
Created December 19, 2012 10:19
Project Euler Problem 17
(require '[clojure.test :refer (is)])
(defn in-words
"Represents the given number in English words without spaces nor hyphens.
This works with a number in the range from 1 to 1000"
[n]
(cond
(< n 20)
(["" "one" "two" "three" "four" "five" "six" "seven" "eight"
"nine" "ten" "eleven" "twelve" "thirteen" "fourteen"
@kohyama
kohyama / pep024+.clj
Last active December 10, 2015 02:58
Project Euler Problem 24
(require '[clojure.test :refer (is)])
(defn pms
"Permutations lexicographic by index."
[[a & [b & [c & _]] :as ls]]
(cond
(nil? a) '()
(nil? b) `(~a)
(nil? c) `((~a ~b) (~b ~a))
:else (mapcat (fn [h] (map (fn [r] (cons h r))
@kohyama
kohyama / pep011.clj
Last active December 10, 2015 04:28
Project Euler Problem 11
(require '[clojure.string :as s])
(require '[clojure.test :refer (deftest run-tests is)])
(defn trans [[& rows]] (apply (partial map list) rows))
(defn flip [mat] (map reverse mat))
(defn rot45 [mat]
(let [w (count (first mat))
h (count mat)
@kohyama
kohyama / pep019.clj
Last active December 10, 2015 18:08
Project Euler Problem 19
;; 閏年かどうか
(defn leap? [year]
(and (zero? (mod year 4))
(or (pos? (mod year 100))
(zero? (mod year 400)))))
;; 西暦で与えられた年の各月の日数のリスト
(defn numbers-of-days [year]
(if (leap? year)
;;Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec