この記事は、Lispアドベントカレンダー2012 の 11日目 の記事です。前日は、nitro_idiot さんがライブコーディングで凄まじい勢いでWebサイトを作る 記事 でした。
今日のネタは、いろいろ考えた挙句 sqlkorma にしました。
;;; 普通の再帰版 | |
(defn fact-1 [n] | |
(if (= n 0) 1 | |
(*' n (fact-1 (dec n))))) | |
;;; 末尾再帰版 | |
(defn fact-2 | |
([n] (fact-2 n 1)) | |
([n acc] | |
(if (= n 0) acc |
;;; Project Euler Problem 1 | |
;;; http://odz.sakura.ne.jp/projecteuler/index.php?cmd=read&page=Problem%201 | |
;;; | |
;;; 10未満の自然数のうち、3 もしくは 5 の倍数になっているものは 3, 5, 6, 9 の4つがあり、 これらの合計は 23 になる。 | |
;;; 同じようにして、1,000 未満の 3 か 5 の倍数になっている数字の合計を求めよ。 | |
;;; 3 もしくは 5の倍数の場合 true、そうでない場合 false を返す filter 判定用関数 | |
(defn fizz-or-buzz? | |
"3か5で割り切れるならtrue、そうでないならfalseを返す。" | |
[n] |
;; ディスパッチ関数 | |
(defn fizzbuzz-dispatch-fn [n] [(zero? (mod n 3)) (zero? (mod n 5))]) | |
;; マルチメソッドの定義(マッチングめちゃ強力!) | |
(defmulti fizzbuzz fizzbuzz-dispatch-fn) | |
(defmethod fizzbuzz [true false] [n] "fizz") | |
(defmethod fizzbuzz [false true] [n] "buzz") | |
(defmethod fizzbuzz [true true] [n] "fizzbuzz") | |
(defmethod fizzbuzz :default [n] n) |
この記事は、Lispアドベントカレンダー2012 の 11日目 の記事です。前日は、nitro_idiot さんがライブコーディングで凄まじい勢いでWebサイトを作る 記事 でした。
今日のネタは、いろいろ考えた挙句 sqlkorma にしました。
(ns hiccup-mod.compiler | |
(:require [hiccup.compiler :as c])) | |
;;; | |
;;; CAUTION!!! hiccup.compiler の end-tag、render-element を書き換える。 | |
;;; | |
;;; public vars | |
(def ^{:dynamic true :doc " default is two white space."} | |
*html-indent-characters* " ") |
;;;A palindromic number reads the same both ways. | |
;;;The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99. | |
;;;Find the largest palindrome made from the product of two 3-digit numbers. | |
;;;左右どちらから読んでも同じ値になる数を回文数という。 2桁の数の積で表される回文数のうち、 | |
;;;最大のものは 9009 = 91 × 99 である。 | |
;;;では、3桁の数の積で表される回文数のうち最大のものはいくらになるか。 | |
(ns projecteuler.problem-4 | |
(:require [clojure.string :as str]) |
;;; In the 20x20 grid below, four numbers along a diagonal line have been marked in red. | |
;;; : | |
;;; (snip) | |
;;; : | |
;;; The product of these numbers is 26 * 63 * 78 * 14 = 1788696. | |
;;; What is the greatest product of four adjacent numbers | |
;;; in any direction (up, down, left, right, or diagonally) in the 20x20 grid? | |
(ns projecteuler.problem-11 | |
(:use clojure.test)) |
;;; By starting at the top of the triangle below and moving to adjacent numbers | |
;;; on the row below, the maximum total from top to bottom is 23. | |
;;; | |
;;; 3 | |
;;; 7 4 | |
;;; 2 4 6 | |
;;; 8 5 9 3 | |
;;; | |
;;; That is, 3 + 7 + 4 + 9 = 23. | |
;;; |
(ns projecteuler.problem-25 | |
(:require [clojure.string :as str] | |
[clojure.math.numeric-tower :as math]) | |
(:use clojure.test)) | |
(defn fib-gen | |
"フィボナッチ数を generate する関数。初期値[1 1]から iterate する前提。" | |
[[a b]] [b (+' a b)]) | |
(def fib-seq |
(defn camel-to-dash | |
"convert `CamelCase` string to dash-connected string." | |
[s] | |
(->> (re-seq #"[A-Z]+[^A-Z]+|[a-z]+[^a-z]+" s) | |
(map str/lower-case) | |
(interpose "-") | |
str/join)) |