これも書くんなら、末尾再帰の方がエエんちゃうの?と思ったケース。
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
;;; SEGMENT-READER | |
;; Scheme-style | |
(defun segment-reader (stream ch n &optional (acc nil)) | |
(labels ((iter (chars curr) | |
(if (char= ch curr) | |
(coerce (reverse chars) 'string) | |
(iter (cons curr chars) (read-char stream))))) | |
(if (< n 1) | |
(reverse acc) | |
(segment-reader stream ch (1- n) | |
(cons (iter nil (read-char stream)) acc))))) |
Doug Hoyte氏は「効率」を考えて
do
を使ってんのかな?と思わせておいて、いきなり普通に再帰する、と言うワケの分からん事をする。この人のスタイルは、ぶっちゃけ支離滅裂なんだよな(苦笑)。例えば、まあ、冗談として聞いて欲しいんですが、マジメに効率考えて
do
を使え、ってのなら徹底して次のようにして書く事も可能なんです。
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
;;; SEGMENT-READER with DO | |
(defun segment-reader (stream ch n) | |
(do ((n n (1- n)) | |
(acc nil | |
(push | |
(do ((curr (read-char stream) | |
(read-char stream)) | |
(chars nil (push curr chars))) | |
((char= ch curr) (coerce (nreverse chars) 'string))) | |
acc))) | |
((< n 1) (nreverse acc)))) |
冗談ですけどね(笑)。こんなコード読むの大変ですし。書くのも大変。ただ、分かって欲しいのは、そもそも
do
の性質からしてlet
が要らないだろって事です。let
が要らなければ本体部も要らない、っつー事です。
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
CL-USER> (segment-reader t #\/ 3) | |
あいう/えおか/きくけ/ | |
("あいう" "えおか" "きくけ") | |
CL-USER> |
0 件のコメント:
コメントを投稿