Created
April 20, 2012 14:53
-
-
Save fbanados/2429272 to your computer and use it in GitHub Desktop.
Laziness en #lang racket
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
#lang racket | |
(define (my-if pred true false) | |
(if | |
(pred) | |
(true) | |
(false))) | |
;(define (infiniteloop x) | |
; (infiniteloop x)) | |
; La siguiente función se quedaría pegada en #lang racket | |
;(my-if #f (infiniteloop 0) 123) | |
(define ones* | |
(cons 1 | |
(λ () ones*))) | |
(define (my-take* n lazy-list) | |
(cond | |
((empty? lazy-list) '()) | |
((= n 0) '()) | |
(else | |
(cons (car lazy-list) | |
(my-take* (- n 1) ((cdr lazy-list))))))) | |
(my-take* 10 ones*) | |
(define ones | |
(cons 1 | |
(delay ones))) | |
(define naturales | |
(cons 0 | |
(delay (my-map add1 naturales)))) | |
(define (intsfrom n) | |
(cons n | |
(delay (my-map add1 (intsfrom n))))) | |
(define (criba lista) | |
(let | |
((n (car (force lista)))) | |
(cons n | |
(delay | |
(my-filter | |
(λ (x) (not (= 0 (modulo x n)))) | |
(criba (cdr (force lista)))))))) | |
(define (my-map f lazy-list) | |
(cond | |
((empty? lazy-list) '()) | |
(else | |
(cons | |
(f (car (force lazy-list))) | |
(delay (my-map f (cdr (force lazy-list)))))))) | |
(define (my-take n lazy-list) | |
(cond | |
((empty? lazy-list) '()) | |
((= n 0) '()) | |
(else | |
(cons (car lazy-list) | |
(my-take (- n 1) (force (cdr lazy-list))))))) | |
(define (my-filter f lazy-list) | |
(cond | |
((empty? lazy-list) '()) | |
(else (if | |
(f (car (force lazy-list))) | |
(cons (car (force lazy-list)) | |
(delay (my-filter f (cdr (force lazy-list))))) | |
(my-filter f (cdr (force lazy-list))))))) | |
(my-take 10 ones) | |
(my-take 10 naturales) | |
(define odds | |
(my-filter odd? naturales)) | |
(my-take 10 odds) | |
(my-take 10 (my-filter even? naturales)) | |
(my-take 10 (criba (intsfrom 2))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment