let, letrec, let*, named let ・・・といろいろややこしいですもんね。
Scheme を始めたころよくわからずメモってます。
最近は「継続」に四苦八苦してます。
取り敢えず、 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
;; capter.14 | |
; leftmost | |
;; (leftmost '(((a) b)(c d))) | |
;; -> a | |
; The Seasoned Schemer | |
(define atom? | |
(lambda (a) | |
(and (not (pair? a)) | |
(not (null? a))))) | |
(define leftmost | |
(lambda (l) | |
(cond | |
((atom? (car l))(car l)) | |
(else (leftmost (car l)))))) | |
(leftmost '(((a) b)(c d))) | |
; -> a | |
(leftmost '(((a)())()(e))) | |
; -> a | |
(leftmost '(((() a)()))) | |
; -> *** ERROR: pair required, but got () | |
; leftmost * The Seasoned Schemer | |
(define leftmost | |
(lambda (l) | |
(cond | |
((null? l)(quote ())) | |
((atom? (car l))(car l)) | |
(else (cond | |
((atom? (leftmost (car l))) | |
(leftmost (car l))) | |
(else (leftmost (cdr l)))))))) | |
(leftmost '(((a) b)(c d))) | |
; -> a | |
(leftmost '(((a)())()(e))) | |
; -> a | |
(leftmost '(((() a)()))) | |
; -> a | |
(define leftmost | |
(lambda (l) | |
(cond | |
((null? l)(quote ())) | |
((atom? (car l))(car l)) | |
(else (let ((a (leftmost (car l)))) | |
(cond | |
((atom? a) a) | |
(else (leftmost (cdr l))))))))) | |
(leftmost '(((a) b)(c d))) | |
; -> a | |
(leftmost '(((a)())()(e))) | |
; -> a | |
(leftmost '(((() a)()))) | |
; -> a |
0 件のコメント:
コメントを投稿