Basic Werk | Blog | Contact


MEMOMEM

Scheme_Let_Over_Lambda



let の中に閉じ込めた lambda は、let で宣言した変数の値を保持している。

つまり "Let Over Lambda" は Closure を生成する。

また、Closure を更に lambda で包むと Closure Generator を定義できる。



;; Counter (Closure) (define cnt (let ((n 0)) (lambda () (set! n (+ n 1)))))
;; Generator (define cnt-generator (lambda () (let ((n 0)) (lambda () (set! n (+ n 1))))))

gosh> (cnt) 1 gosh> (cnt) 2 gosh> (cnt) 3
gosh> (define cnt1 (cnt-generator)) cnt1 gosh> (define cnt2 (cnt-generator)) cnt2 gosh> (cnt1) 1 gosh> (cnt2) 1 gosh> (cnt1) 2 gosh> (cnt1) 3 gosh> (cnt2) 2


参考:

素数夜曲: 女王陛下のLISP

P.439~


#Scheme #Lisp #Gauche #Closure #Generator



SN 2013/07/12 23:47:36

Archives > Scheme_Let_Over_Lambda.html





20130630_Gauche_study



Gauche の勉強メモ



(define html "<p>This is a a a <em>text</em>.</p>")
;; Perl でいうところの、 ;; $foo =~ s/regex/repracement/g; (regexp-replace-all #/<.+?>/ html "") ;; -> "This is a a a text."
;; グローバルマッチでマッチした文字列のリストを返す (use gauche.generator)
(define (m/re/g re str) (map rxmatch-substring (generator->list (grxmatch re str))))
(m/re/g #/\w+/ html) ;; -> ("p" "This" "is" "a" "em" "text" "em" "p")
;; 重複したリストを省く (delete-duplicates (m/re/g #/\w+/ html)) ;; -> ("p" "This" "is" "a" "em" "text")
;; 例えばこんな感じで ;; ブラックリストを作っておき (define (is_ng? x) (if (< (string-length x) 2) #t #f))
;; ブラックリストを省く (remove is_ng? (delete-duplicates (m/re/g #/\w+/ html))) ;; -> ("This" "is" "em" "text")


文字列の出力、日付、format 数値左0埋め。



;; Perl でいうところの ;; open my $out, ">", "file"; (define out (open-output-file "file" :if-exists :supersede)) (format out "This is a text.\n")
;; 日付は srfi-19 を use する (use srfi-19) (define today (current-date)) (format out "And Today is ~4,'0D/~2,'0D/~2,'0D.\n" (date-year today) (date-month today) (date-day today))
;; Perl でいうところの ;; close $out; (close-output-port out)
;; file の中身は ;; This is a text. ;; And Today is 2013/07/01.


参考:

6.13 正規表現

http://practical-scheme.net/gauche/man/gauche-refj_51.html


9.8.2 Generator operations

http://practical-scheme.net/gauche/man/gauche-refj_82.html#Generator-operations


リストから重複する要素を取り除く - Gaucheクックブック

http://d.hatena.ne.jp/rui314/20070219/p1


6.22.8 出力

http://practical-scheme.net/gauche/man/gauche-refj_60.html#g_t_00e5_0087_00ba_00e5_008a_009b


#Gauche #Scheme #Lisp #Regex



SN 2013/07/01 00:55:19

Archives > 20130630_Gauche_study.html