Skip to content

Instantly share code, notes, and snippets.

View nnabeyang's full-sized avatar

noriaki watanabe nnabeyang

View GitHub Profile
@nnabeyang
nnabeyang / gcd.hs
Last active August 29, 2015 13:55
ユークリッド互除法の計算過程をControl.Monad.Writerで ref: http://qiita.com/nnabeyang/items/4667ed2ed5aa2a618e28
import Control.Monad.Writer
gcd' :: Int -> Int -> Writer [String] Int
gcd' a b
|b == 0 = do
tell["= " ++ show a]
return(a)
|otherwise = do
let nb = a `mod` b
expr = "gcd(" ++ show(b) ++ ", " ++ show(nb) ++ ")"
msg = show(a) ++ " = " ++ show(b) ++ " * " ++ show(a `div` b) ++ " + " ++ show(nb)
@nnabeyang
nnabeyang / hanoi.hs
Created January 30, 2014 07:53
左端のn段のハノイの塔を最小回数で右端に移動させるときの最初の一手について ref: http://qiita.com/nnabeyang/items/5339a3248284b0039255
import Control.Monad.Writer
size :: [[a]] -> Int
size = foldr (\ xs n -> length xs + n) 0
move :: [[Int]] -> (Int, Int) -> Writer [String] [[Int]]
move xss (from, to) = return([move_ xs y (from, to) i| (xs, i) <- zip xss [0..]])
where (y:_) = xss !! from
move_ :: [Int] -> Int -> (Int, Int) -> Int -> [Int]
move_ [] y (from, to) i = if i == to then [y] else []
move_ (x:xs) y (from, to) i |i == from = xs
@nnabeyang
nnabeyang / gemstring.hs
Last active August 29, 2015 13:56
『王女様の宝石パターンを見つけよう!』 https://codeiq.jp/ace/yuki_hiroshi/q684
import Parsing (parse, line)
import System.IO
import System.Environment
import Data.Char
import Data.Map as M
import Data.Time.Clock
import Control.Monad.State
-- http://d.hatena.ne.jp/mzp/20090308/bench
bench f = do from <- getCurrentTime
@nnabeyang
nnabeyang / prime.hs
Created February 18, 2014 13:13
素数判定
ldf :: Int -> Int -> Maybe Int
ldf k n | k ^ 2 > n = Nothing
| rem n k == 0 = Just k
| otherwise = ldf (k+1) n
prime :: Int -> Bool
prime n = case (ldf 2 n) of
Just m -> m == n
Nothing -> True
$ sqlite3 test.db
SQLite version 3.7.9 2011-11-01 00:52:41
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> create table fruits(name string, value integer);
sqlite> insert into fruits values('apple', 100);
sqlite> insert into fruits values('orange', 130);
sqlite> .q
@nnabeyang
nnabeyang / git_log_reverse_p.txt
Created March 24, 2012 08:00
GitのNotesで文章を管理
commit 917538814d304a01b750db8c84d3fe0e08c9e435
Author: noriaki watanabe <nabeyang@gmail.com>
Date: Thu Mar 22 09:12:22 2012 +0900
initial commit
diff --git a/src/unit.rb b/src/unit.rb
new file mode 100644
index 0000000..e69de29
@nnabeyang
nnabeyang / README.md
Created April 30, 2012 08:15
wiki2html_windows

wiki2html -- A wiki to html converter

wiki2htmlについて

wiki2htmlはlivedoor wikiのような記法で書かれた テキストファイルをHTMLに変換するツールです。以下の方針に従って開発しています。

  • できるだけ多くのエラーを発見する
  • 設定ファイルによる記法の変更可能性
  • HTMLが生成されるまでの時間は遅くない
@nnabeyang
nnabeyang / myconcern.rb
Created July 25, 2012 12:50
複数のモジュールにあるStaticeMethodsを1つのクラスのクラスメソッドにまとめる
module Concern
def self.extended(base)
base.instance_variable_set("@dependencies", [])
end
def append_features(base)
if base.instance_variable_defined?("@dependencies")
base.instance_variable_get("@dependencies") << self
else
super
@dependencies.each {|dep| base.send(:include, dep)}
@nnabeyang
nnabeyang / myrails.rb
Created July 25, 2012 13:15
validatesの簡易版
class Class
def class_attribute name
class_eval <<-RUBY, __FILE__, __LINE__ + 1
def self.#{name}() nil end
def self.#{name}=(val)
singleton_class.class_eval {
undef_method :#{name}
define_method(:#{name}) { val }
}
val
@nnabeyang
nnabeyang / controller_and_view.rb
Created July 29, 2012 16:04
railsのコントローラーとビューの連携はこんな感じ
require 'erb'
class ERB
def self.call(source)
new(source, nil, nil, '@output_buffer').src
end
end
class Template
def initialize(source, path)
@path = path
@source = source