Chat (Lingr.com)
Informaiton
Daily
Column
- MySQL日本語の旅(5/1)
- アクセス向上秘伝(5/9)
- 一風変ったHaskellλ門(6/13)
- SICP Answer Book (5/31) 問題3.26追加
Zope Solution
Extra
アーカイブ
OSS案内所
Site Info
関連リンク
- 2004: 01 02 03 04 05 06 07 08 09 10 11 12
- 2005: 01 02 03 04 05 06 07 08 09 10 11 12
- 2006: 01 02 03 04 05 06 07 08 09 10 11 12
- 2005-10-31 [VMWare] VMWare Playerで新たなOSをインストールする
- 2005-10-28 [SSL] tcpserver の SSL 化
- 2005-10-27 [Vine Linux] iptables
- 2005-10-26 [Firefox] JavaScriptなどが動的に生成したHTMLのソースを見る方法
- 2005-10-25 [JavaScript] JavaScriptの配列型のlengthプロパティ
- 2005-10-24 [Emacs] ECMAScript-modeの導入
- 2005-10-21 [bash] 今月の日曜日の数
- 2005-10-20 [Gauche] Rose Tree (続々々)
- 2005-10-19 [Gauche] Rose Tree (続々)
- 2005-10-18 [Gauche] Rose Tree (続)
- 2005-10-17 [Gauche] Rose Tree
- 2005-10-14 [Haskell] Ascii Table
- 2005-10-13 [Gauche] ツリーのマップ
- 2005-10-12 [Gauche] たたみこみ
- 2005-10-11 [SRFI-10] リーダーマクロ
- 2005-10-07 [Graph] グラフ的列
- 2005-10-06 [Haskell] 正規表現の \w にマッチする文字のリスト
- 2005-10-05 [tcpserver] サーバ化ツール
- 2005-10-04 [tcpclient] telnet の代用
- 2005-10-03 [ucspi-tcp] Vine Linux へのインストール
2005-10-31 [VMWare] VMWare Playerで新たなOSをインストールする
http://blog.yasaka.com/archives/2005/10/vmware_playervi.html
要点は、
- 既存のVMWareイメージを実行して新たなOSをインストールすれば良い
- インストールCDがあればそのままインストールできる
- ISOイメージを読み込ませるには設定ファイルの編集が必要
--yasuyuki
2005-10-28 [SSL] tcpserver の SSL 化
tcpserver をSSL化するためのパッチというのが存在する.これは便利かも.
http://www.nrg4u.com/qmail/ucspi-tcp-ssl-20050405.patch.gz
--nobsun
There is no comment.
2005-10-27 [Vine Linux] iptables
個人のPC環境をグローバルなネットワークに臨時に繋ぎたいとき, 予期せぬアクセスを受けつけないように設定しておきたい. とりあえず.
# iptables -F # iptables -P INPUT DROP # iptables -P FOWARD DROP # iptables -A INPUT -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT # iptables -A INPUT -m state --state ESTABLISHE,RELATED -j ACCEPT
としておくと良いかも...
--nobsun
There is no comment.
2005-10-26 [Firefox] JavaScriptなどが動的に生成したHTMLのソースを見る方法
JavaScriptなどが動的にHTMLを生成している場合、 Filefoxの[表示]-[ページのソース]では表示できない。
こんなとき、表示したい部分をマウスで選択して右クリックし、 コンテキストメニューから[選択した部分のソースを表示]を選ぶと表示できる。
ページ全体のソースを表示したい場合は[Ctrl]+[A]でページ全体を選択して右クリックする。
--yasuyuki
JavaScriptコンソールから無理矢理一行で
document.write("<textarea style='width:100%;height:100%'>"+new XMLSerializer().serializeToString(document.documentElement)+"</textarea>")
2005-10-25 [JavaScript] JavaScriptの配列型のlengthプロパティ
JavaScriptの配列型のlengthプロパティは読み取り専用ではない。
<html>
<head>
<script language="JavaScript">
function array_test() {
var array = new Array(1, 2, 3, 4, 5);
alert('array.length='+array.length); // 'array.length=5'
alert('array[2]='+array[2]); // 'array[2]=3'
array.length = 0;
alert('array.length='+array.length); // 'array.length=0'
alert('array[3]='+array[3]); // 'array[2]=undefined'
}
</script>
</head>
<body onload="array_test()">
</html>
配列型のlengthプロパティに値を代入することによって、 配列要素を削除することができる。
--yasuyuki
There is no comment.
2005-10-24 [Emacs] ECMAScript-modeの導入
EmacsでJavaScriptを編集するとき便利なECMAScript-modeを導入する。
http://www.emacswiki.org/cgi-bin/wiki/ecmascript-mode.el
上記から ecmascript-mode.el をダウンロードし、 $HOME/lib/emacsにコピーしておく。
.emacs.elに以下を記述する。
(setq load-path (append '("~/lib/emacs") load-path))
;;; ECMAScript
(autoload 'ecmascript-mode "ecmascript-mode" "ECMAScript" t)
(setq auto-mode-alist
(append '(("\\.js$" . ecmascript-mode))
auto-mode-alist))
拡張子 .js ファイルを読み込むとECMAScript-modeが動作する。
--yasuyuki
There is no comment.
2005-10-21 [bash] 今月の日曜日の数
すこし前に,はてなにでていた質問. 「Rubyのプログラムで今月「日曜日」が何回あるのか教えてください。(「今月」はプログラム実行時のシステム時間でかまいません)」
寄せられている解答はどれもなるほどというものでしたが,その中で 「うまいっ!」とおもったのは Bash の oneliner
$ cal | cut -b 1-2 | grep -c [0-9] 5
--nobsun
cal | grep -c '^.[0-9]' # cut 使わなくてもよさそう
2005-10-20 [Gauche] Rose Tree (続々々)
えっ.ノードパスは,出現番号で欲しいって? どうゆこと?
("ROOT" "a" "e")
じゃなくて,
(0 1 5)
が欲しいの?わがままだなぁ.(誰がやねん^^;)
(define (find-rose-tree-num-path count path tree pred)
(match tree
(`(Rose ,content ,children)
(if (pred tree)
(values count (reverse (cons count path)))
(if (null? children)
(values count path)
(find-rose-forest-num-path count (cons count path) children pred))))))
(define (find-rose-forest-num-path count path forest pred)
(if (null? forest)
(values count (cdr path))
(receive (new-count new-path)
(find-rose-tree-num-path (+ 1 count) path (car forest) pred)
(if (> (length new-path) (length path))
(values new-count new-path)
(find-rose-forest-num-path new-count path (cdr forest) pred)))))
こんな感じでどう?
gosh> (find-rose-tree-num-path 0 '() test-tree (compose (cut string=? "e" <>) rose-tree-content)) 5 (0 1 5)
...lazy ならもっと楽なのにぃ...ぼそっ...
--nobsun
There is no comment.
2005-10-19 [Gauche] Rose Tree (続々)
ルートから特定のノードまでのパスが知りたい.
(define (find-rose-tree-path tree p)
(match tree
(`(Rose ,content ,children)
(receive (_ as) (break p children)
(if (null? as)
(cond ((find-rose-forest-path children p)
=> (cut cons tree <>))
(else #f))
(cons tree (list (car as))))))))
(define (find-rose-forest-path forest p)
(if (null? forest)
#f
(or (find-rose-tree-path (car forest) p)
(find-rose-forest-path (cdr forest) p))))
で,
gosh> (define test-tree
(rose-tree "ROOT"
(list (rose-tree "a"
(list (rose-tree "b"
(list (rose-tree "c" '())
(rose-tree "d" '())))
(rose-tree "e"
(list (rose-tree "f" '())))))
(rose-tree "g" '())
(rose-tree "h" '()))))
test-tree
gosh> (map rose-tree-content (find-rose-tree-path
test-tree
(compose (cut string=? "e" <>) rose-tree-content)))
("ROOT" "a" "e")
gosh>
でどうよ.
--nobsun
There is no comment.
2005-10-18 [Gauche] Rose Tree (続)
部分木の追加と削除(もちろん!?副作用なし)
(define (add-rose-tree-sibling-before tree p new-sibling)
(let ((content (rose-tree-content tree))
(children (rose-tree-children tree)))
(receive (bs as) (break p children)
(if (null? as)
(cond ((add-rose-forest-sibling-before children p new-sibling)
=> (cut rose-tree content <>))
(else #f))
(rose-tree content
(append bs (cons new-sibling as)))))))
(define (add-rose-forest-sibling-before forest p new-sibling)
(if (null? forest)
#f
(or (cond ((add-rose-tree-sibling-before (car forest) p new-sibling)
=> (cut cons <> (cdr forest)))
(else #f))
(cond ((add-rose-forest-sibling-before (cdr forest) p new-sibling)
=> (cut cons (car forest) <>))
(else #f)))))
(define (add-rose-tree-sibling-after tree p new-sibling)
(let ((content (rose-tree-content tree))
(children (rose-tree-children tree)))
(receive (bs as) (break p children)
(if (null? as)
(cond ((add-rose-forest-sibling-after children p new-sibling)
=> (cut rose-tree content <>))
(else #f))
(rose-tree content
(append bs (cons (car as) (cons new-sibling (cdr as)))))))))
(define (add-rose-forest-sibling-after forest p new-sibling)
(if (null? forest)
#f
(or (cond ((add-rose-tree-sibling-after (car forest) p new-sibling)
=> (cut cons <> (cdr forest)))
(else #f))
(cond ((add-rose-forest-sibling-after (cdr forest) p new-sibling)
=> (cut cons (car forest) <>))
(else #f)))))
(define (delete-rose-tree-sibling tree p)
(let ((content (rose-tree-content tree))
(children (rose-tree-children tree)))
(receive (bs as) (break p children)
(if (null? as)
(cond ((delete-rose-forest-sibling children p)
=> (cut rose-tree content <>))
(else #f))
(rose-tree content (append bs (cdr as)))))))
(define (delete-rose-forest-sibling forest p)
(if (null? forest)
#f
(or (cond ((delete-rose-tree-sibling (car forest) p)
=> (cut cons <> (cdr forest)))
(else #f))
(cond ((delete-rose-forest-sibling (cdr forest) p)
=> (cut cons (car forest) <>))
(else #f)))))
たとえば,
gosh> (define test-tree
(rose-tree "ROOT"
(list (rose-tree "a"
(list (rose-tree "b"
(list (rose-tree "c" '())
(rose-tree "d" '())))
(rose-tree "e"
(list (rose-tree "f" '())))))
(rose-tree "g" '())
(rose-tree "h" '()))))
test-tree
としておいて,
gosh> (define hoge (rose-tree "HOGE" '()))
hoge
gosh> (define foo
(add-rose-tree-sibling-before
test-tree
(compose (cut string=? "b" <>) rose-tree-content)
hoge))
foo
gosh> (define bar
(add-rose-tree-sibling-after
test-tree
(compose (cut string=? "b" <>) rose-tree-content)
hoge))
bar
gosh> (define baz
(delete-rose-tree-sibling
test-tree
(compose (cut string=? "b" <>) rose-tree-content)))
baz
gosh> (define (show-tree tree)
(for-each-rose-tree pp (label-rose-tree '() tree)))
show-tree
gosh> (for-each show-tree (list test-tree foo bar baz))
ROOT
* 1 a
** 1.1 b
*** 1.1.1 c
*** 1.1.2 d
** 1.2 e
*** 1.2.1 f
* 2 g
* 3 h
ROOT
* 1 a
** 1.1 HOGE
** 1.2 b
*** 1.2.1 c
*** 1.2.2 d
** 1.3 e
*** 1.3.1 f
* 2 g
* 3 h
ROOT
* 1 a
** 1.1 b
*** 1.1.1 c
*** 1.1.2 d
** 1.2 HOGE
** 1.3 e
*** 1.3.1 f
* 2 g
* 3 h
ROOT
* 1 a
** 1.1 e
*** 1.1.1 f
* 2 g
* 3 h
gosh>
--nobsun
There is no comment.
2005-10-17 [Gauche] Rose Tree
子のツリーを1つ以上持つかあるい1つも持たないノードで構成される木を多分木 (multi-way tree)という.multi-way tree は rose tree と呼ばれることもある. (なぜ rose tree と呼ばれるのでしょう.知っていたら教えてくださいませ.)
rose tree 用にちょっとした関数群を作ってみた,
(use util.match)
(use gauche.collection)
(use srfi-13)
(define (rose-tree content children) ;; 構成子
`(Rose ,content ,children))
(define rose-tree-content ;; 選択子
(match-lambda (`(Rose ,c ,_) c) (else #f)))
(define rose-tree-children ;; 選択子
(match-lambda (`(Rose ,_ ,cs) cs) (else #f)))
(define (map-rose-tree f tree) ;; map
(rose-tree (f (rose-tree-content tree))
(map (cut map-rose-tree f <>)
(rose-tree-children tree))))
(define (for-each-rose-tree f tree) ;; for-each
(begin
(f (rose-tree-content tree))
(for-each-rose-forest f (rose-tree-children tree))))
(define (for-each-rose-forest f forest)
(if (null? forest)
(values)
(begin
(for-each-rose-tree f (car forest))
(for-each-rose-forest f (cdr forest)))))
(define (find-rose-tree p tree) ;; find
(match tree
(`(Rose ,c ,cs)
(cond ((p c) tree)
((null? cs) #f)
(else (or (find-rose-tree p (car cs))
(find-rose-forest p (cdr cs))))))))
(define (find-rose-forest p forest)
(if (null? forest)
#f
(or (find-rose-tree p (car forest))
(find-rose-forest p (cdr forest)))))
(define (label-rose-tree label tree) ;; hierarchical labeling
(rose-tree (list label (rose-tree-content tree))
(receive
(forest _)
(label-rose-forest (cons 1 label) (rose-tree-children tree))
forest)))
(define (label-rose-forest label forest)
(map-accum (lambda (tree lab)
(values (label-rose-tree lab tree)
(cons (+ (car lab) 1) (cdr lab))))
label
forest))
でもって,たとえば,
(define test-tree
(rose-tree "ROOT"
(list (rose-tree "a"
(list (rose-tree "b"
(list (rose-tree "c" '())
(rose-tree "d" '())))
(rose-tree "e"
(list (rose-tree "f" '())))))
(rose-tree "g" '())
(rose-tree "h" '()))))
と test-tree を定義して, (for-each-rose-tree pp (label-rose-tree '() test-data)) を評価すると
gosh> (for-each-rose-tree pp (label-rose-tree '() test-data)) ROOT * 1 a ** 1.1 b *** 1.1.1 c *** 1.1.2 d ** 1.2 e *** 1.2.1 f * 2 g * 3 h
ここでクイズ(易).pp を定義せよ.
--nobsun
There is no comment.
2005-10-14 [Haskell] Ascii Table
たとえば,
% cat csv.data haga,325,h hoge,52,dj foo,2,rw3
というCSVデータがあったとき,これをAscii Artの罫線で表にするスクリプト
% runhaskell csv2atable.hs < csv.data +----+---+---+ |haga|325|h | +----+---+---+ |hoge|52 |dj | +----+---+---+ |foo |2 |rw3| +----+---+---+
で,そのコード. readCVSはちょっと手抜きだけど,まぁまぁ使えそう.
import Data.Char
import Data.List
main :: IO ()
main = getContents >>= mapM_ putStrLn . csvToAsciiTable . lines
csvToAsciiTable :: [String] -> [String]
csvToAsciiTable ls = sandwitch hline $ map (tr lens) csv
where csv = map readCSV ls
lens = map (maximum . map length) (transpose csv)
hline = concat $ sandwitch "+" $ map (flip replicate '-') lens
tr ls cs = concat $ sandwitch "|" $ zipWith ljustify ls cs
sandwitch :: a -> [a] -> [a]
sandwitch x = foldr (\ y s -> x:y:s) [x]
ljustify :: Int -> String -> String
ljustify n s = take n $ s ++ repeat ' '
readCSV :: String -> [String]
readCSV = map trim . separate ','
separate :: Eq a => a -> [a] -> [[a]]
separate s [] = []
separate s xs = case break (s ==) xs of
(ps,[]) -> [ps]
(ps,_:ys) -> ps:separate s ys
trim :: String -> String
trim = reverse . dropWhile isSpace . reverse . dropWhile isSpace
--nobsun
There is no comment.
2005-10-13 [Gauche] ツリーのマップ
たとえば,
a
|
+- b
| |
| +- c
| | |
| | +- d
| | |
| | +- e
| | |
| | +- f
| |
| +- g
| | |
| | +- h
| | |
| | `- i
| |
| `- j
|
`- k
|
`- l
というツリー構造は,S式では
'(a (b (c (d e f) g (h i) j) k (l)))
と表現できる.この構造を変えずに a -> A のように変換したいことがままあ る.そこで,map-tree をつくった.
(define (map-tree leaf? f tree)
(cond ((null? tree) '())
((leaf? tree) (f tree))
(else
(cons (map-tree leaf? f (car tree))
(map-tree leaf? f (cdr tree))))))
使い方は
gosh> (use srfi-13) #<undef> gosh> (map-tree symbol? (lambda (s) (string->symbol (string-upcase (x->string s)))) '(a (b (c (d e f) g (h i) j) k (l)))) (A (B (C (D E F) G (H I) J) K (L)))
--nobsun
There is no comment.
2005-10-12 [Gauche] たたみこみ
Haskell の Data.List.mappAccumL 相当.どこかにあったような気もするが...
(define (mapAccumL f s0 xxs)
(if (null? xxs)
(values s0 '())
(receive (x xs) (car+cdr xxs)
(receive (s1 y) (f s0 x)
(receive (s2 ys) (mapAccumL f s1 xs)
(values s2 (cons y ys)))))))
--nobsun
gauche.collectionにあるmap-accumが使えるのでは。
あっ。やっぱりあったか。。。^^;
2005-10-11 [SRFI-10] リーダーマクロ
readした瞬間に置き換えるマクロ。
(define-class <point> ()
((x :init-keyword :x :init-value 0)
(y :init-keyword :y :init-value 0)))
(define-reader-ctor 'point
(lambda (x y)
(make <point> :x x :y y)))
なんてしておいて、
gosh> #,(point 3 4) #<<point> 0x80fa058> gosh> '(#,(point 10 20) #,(point 100 200)) (#<<point> 0x8105c70> #<<point> 0x8105bd8>)
二つめのはクウォートしてるんだけど、readした瞬間に評価されてる。
--cut-sea
There is no comment.
2005-10-07 [Graph] グラフ的列
mput の日記。より,
それぞれの頂点のから枝がd1本, d2本, ... , dn本出ているようなn頂点の 単純無向グラフGが存在する場合に、非負整数の配列[d1, d2, ... , dn]は グラフ的(Graphical)であるという。 たとえば
[1, 2, 2, 3, 4]はグラフ的である。 ...図略...
- 問1: [5, 2, 3, 2, 3, 2, 5]はグラフ的か?
- 問2: 任意の配列がグラフ的かを調べるArray#graphical?をRubyで実装せよ
- 問3: Haskellで実装せよ
で,問3をやってみた.(自信全然なし)
split1 :: [a] -> [([a],[a])]
split1 [x] = []
split1 (x:xs) = ([x],xs) : map (\ (ys,zs) -> (x:ys,zs)) (split1 xs)
isGraphic ds = (and $ map (0 <=) ds)
&& even (sum ds)
&& ( and
$ zipWith (\ n (xs,ys)
-> sum xs <= n*(n-1)
+ foldl (\ s x -> s + min n x) 0 ys)
[1..length ds - 1]
(split1 $ reverse $ sort ds))
--nobsun
美しい解は、hanataniさんのやつ http://www.lab2.kuis.kyoto-u.ac.jp/~hanatani/tdiary/?date=20051007 それをすこし改訂したやつは http://www.sampou.org/cgi-bin/haskell.cgi?Programming%3a%b6%cc%bc%ea%c8%a2%3a%a4%bd%a4%ce%c2%be
2005-10-06 [Haskell] 正規表現の \w にマッチする文字のリスト
この質問へのHaskellでの回答
% ghc -e "'_':filter Char.isAlphaNum ['0'..'z']"
--nobsun
There is no comment.
2005-10-05 [tcpserver] サーバ化ツール
tcpclient があるなら当然 tcpserver はあるよね.(というか,tcpserver の ほうがメインかも.)
tcpserver は
tcpserver [options] host port program
のように使う.host はサービスを行うIPアドレスまたはFQDN(特に指定しない ときは 0). port はサービスを行うポート.program には標準入力から読み込 み,標準出力に書きだすプログラムを指定する.tcpserver は指定しだいで, アクセス制御を丁寧におこなってくれるので大変便利.
以下は「超」簡単 echo サーバを起動したところ.
% tcpserver -v 0 9999 sh -c cat tpcserver: status: 0/40
これに tcpclient を使ってアクセスして hoge を送信する
% tcpclient 127.0.0.1 9999 sh -c 'cat<&6 & cat >&7' hoge ← 入力 hoge ← 出力
サーバを起動したターミナルには
tcpserver: status: 1/40 tcpserver: pid 19402 from 127.0.0.1 tcpserver: ok 19402 localhost:127.0.0.1:9999 localhost:127.0.0.1::36093
のようなメッセージが表示される.
--nobsun
% tcpserver -v 0 9999 cat でいいのでは? tcpclientの例と対応させるためにわざとでしょうか。
f(^^;) あれこれフィルターなぞいれてとおもってたんですが、結局、cat だけになって しまったというのがオチですた。
2005-10-04 [tcpclient] telnet の代用
tcpclient は基本的に
% tcpclient [options] host port program
という具合でつかう.host と port はそれぞれ接続先のアドレスとポートで ある.program はディスクリプタ番号6(読み込み),7(書き出し)を使うプログ ラムにする.telnetと同じように,コンソールからメッセージを書き込み, コンソールへ応答を出力するには,たとえば,
% tcpclient www.example.org 80 sh -c 'cat < &6 & cat > &7' GET / HTTP/1.0 HTTP/1.1 200 OK Date: Tue, 04 Oct 2005 00:49:37 GMT Server: Apache ...
などとする.
--nobsun
There is no comment.
2005-10-03 [ucspi-tcp] Vine Linux へのインストール
tcpserver と tcpclient という便利なツールが含まれている D.J.Berstein によるツール群.インストールは
% tar zxf ucspi-tcp-0.88.tar.gz % cd ucspi-tcp-0.88 % make % sudo make setup check
でいいんだけど,Vine Linux 3.2 では
Incorrectly built binary which accesses errno, h_errno or _res directly. Needs to be fixed.
とかいわれてしまう.これを回避するには, ucspi-tcpのソースのerror.h中で
extern int errno;
となっているところを
#include <error.h>
に変更する
--nobsun
There is no comment.