Twitter / いわた: fold「オレにだって……できないことぐらい…ある…」そしてこれ↓を知らなかった事実。
;; named let構文 (let name ((var expr) ...) body ...) ;; これは次のコードと等価 (letrec ((name (lambda (var ...) body ...) )) (name expr ...))
ちょいと試しに・・・
fold
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
; fold | |
(define (my-reverse ls) | |
(fold cons '() ls)) | |
(my-reverse '(1 2 3 4 5)) | |
; -> (5 4 3 2 1) |
letrec
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
; letrecc | |
(define (my-reverse ls) | |
(letrec ((r (lambda (l acc) | |
(if (null? l) | |
acc | |
(r (cdr l) | |
(cons (car l) acc)))))) | |
(r ls '()))) | |
(my-reverse '(1 2 3 4 5)) | |
; -> (5 4 3 2 1) |
named 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
; named let | |
(define (my-reverse ls) | |
(let loop ((l ls) | |
(acc '())) | |
(if (null? l) | |
acc | |
(loop (cdr l) | |
(cons (car l) acc))))) | |
(my-reverse '(1 2 3 4 5)) |
0 件のコメント:
コメントを投稿