This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# encoding: utf-8 | |
# str の位置 i から読み込んで対応する ( , ) の位置を表す | |
# 三つ組の配列と、終了した位置を返す。 | |
# | |
# kakko_ten_kokka("(,)", 0) | |
# => [[[0,1,2]], 3] | |
def kakko_ten_kokka(str, i) | |
if str[i] == '(' | |
kakko = i |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# encoding: utf-8 | |
def show(val) | |
if val.is_a? Array | |
"(" + val.map {|x| show(x)}.join(',') + ")" | |
elsif val.is_a? String | |
val | |
else | |
raise TypeError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
# encoding: utf-8 | |
=begin | |
(,)言語の文に対して (A*B)*C -> A*(B*C) 変換を行う。 | |
(... , ...) の形の物をコンマ式と呼ぶ。 | |
... の部分は空であるか、別のコンマ式である。 | |
=end | |
class CommaExp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Keyboard | |
@@buf = [] | |
def self.getchar | |
if @@buf.empty? | |
@@buf += gets.chars | |
end | |
@@buf.shift | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
adjacent = { | |
:A => %i(B C), | |
:B => %i(A C D), | |
:C => %i(A B E), | |
:D => %i(B E F), | |
:E => %i(C D G), | |
:F => %i(D), | |
:G => %i(E) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun nub (lst) | |
(cond | |
((null lst) nil) | |
(t (destructuring-bind (x &rest xs) lst | |
(cons x (nub (remove-if (lambda (y) (equal y x)) xs))))))) | |
(defun iota (n m) | |
(if (> n m) | |
nil | |
(cons n (iota (1+ n ) m)))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defun zip (&rest llist) | |
(if (some #'null llist) | |
nil | |
(cons (mapcar #'car llist) | |
(apply #'zip (mapcar #'cdr llist))))) | |
(defun list-last (ls) | |
(car (last ls))) | |
(defun but-last (ls) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# 手続的 | |
def slice_before1 enum, pred | |
result = [] | |
this = [] | |
enum.each do |item| | |
if pred.call item | |
result << this unless this.empty? | |
this = [item] | |
else |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; start で始まり end で終わる数値のリストを作る。 | |
(defun iota (start end) | |
(if (<= start end) | |
(cons start (iota (1+ start) end)) | |
nil)) | |
;; 代名詞 % を使って式を細かく区切るマクロ。 | |
(defmacro ->% (dat &rest xs) | |
(if xs | |
`(->% ,(substitute dat '% (car xs)) ,@(cdr xs)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; 目的: ツリーの葉を置き換える。 | |
;; | |
;; 例: (rsub 'x 'a '(9 (a '(a b) c) 10)) | |
;; => (9 (X '(X B) C) 10) | |
;; | |
;; 引数: オブジェクト, オブジェクト, リスト -> リスト | |
;; | |
(defun rsub (new old ls) | |
(mapcar (lambda (item) | |
(if (equal item old) |
OlderNewer