#lang racket (require (lib "trace.ss")) (define (stream-zip-with f . argstreams) ;; merge pe oricâte argumente, nu se poate copia în loc de funcția din lab5 (if (stream-empty? (car argstreams)) empty-stream ;; un flux cu (f first1 first2 ... firstn) si apel rec (stream-cons (apply f (map stream-first argstreams)) (apply stream-zip-with f (map stream-rest argstreams))))) ;---------------GRAFURI INFINITE-------------------- ;---FĂRĂ STRUCTURI SPECIALE--- ;(define g ; (letrec ((a (list 'a a b)) ; (b (list 'b b a))) ; a)) ;---CU λ()--- (define counter 0) (define g1 (letrec ((a (λ () (set! counter (add1 counter)) (list 'a a b))) (b (λ () (list 'b b a)))) (a))) ;g1 ;(define key first) ;(define (ls node) ((second node))) ;(define (rs node) ((third node))) ;g1 ;(equal? g1 (ls g1)) ;(eq? g1 (ls g1)) ;counter ;---CU PROMISIUNI--- ;(define c 0) ;(define g2 ; (letrec ((a (delay (set! c (add1 c)) (list 'a a b))) ; (b (delay (list 'b b a)))) ; (force a))) ;(define key car) ;(define (ls node) (force (second node))) ;(define (rs node) (force (third node))) ;g2 ;(equal? g2 (ls g2)) ;(eq? g2 (ls g2)) ;c ;---------------LISTE VS FLUXURI------------------- ;---MODULAR PE LISTE--- (define (interval a b) (if (> a b) '() (cons a (interval (add1 a) b)))) (define (prime? n) (null? (filter (λ (d) (zero? (modulo n d))) (interval 2 (sqrt n))))) ;(filter prime? (interval 2 10)) ;(prime? 100000000000000) ;---ITERATIV PE LISTE--- (define (ugly-prime? n) (let iter ((factor 2)) (cond ((> (* factor factor) n) #t) ((zero? (modulo n factor)) #f) (else (iter (add1 factor)))))) ;(ugly-prime? 100000000000000) ;---MODULAR PE FLUXURI--- (define (interval-stream a b) (if (> a b) empty-stream (stream-cons a (interval-stream (add1 a) b)))) (define (prime-stream? n) (stream-empty? (stream-filter (λ (d) (zero? (modulo n d))) (interval-stream 2 (sqrt n))))) ;(trace interval-stream) ;(prime-stream? 115) ;(prime-stream? 100000000000000) ;---FUNCȚIE DE PRELUARE ÎNTR-O LISTĂ A N ELEMENTE DIN FLUX--- ;-----------UTILĂ LA AFIȘAREA FLUXULUI----------------------- (define (stream-take s n) (cond ((zero? n) '()) ((stream-empty? s) '()) (else (cons (stream-first s) (stream-take (stream-rest s) (- n 1)))))) ;--------FLUXURI DEFINITE EXPLICIT------------- ;---NATURALS--- (define naturals (let loop ((n 0)) (stream-cons n (loop (add1 n))))) ;naturals ;(stream-take naturals 10) ;---FACTORIALS--- (define factorials (let loop ((n 1) (fact 1)) (stream-cons fact (loop (add1 n) (* n fact))))) ;factorials ;(stream-take factorials 10) ;---FIBONACCI--- (define fibonacci (let loop ((a 0) (b 1)) (stream-cons a (loop b (+ a b))))) ;(stream-take fibonacci 10) ;--------FLUXURI DEFINITE IMPLICIT------------- ;---ONES--- (define ones (stream-cons 1 ones)) ;ones ;(stream-take ones 10) ;---NATURALS--- (define naturals- (stream-cons 0 (stream-zip-with + ones naturals-))) (define naturals-- (stream-cons 0 (stream-map add1 naturals--))) ;naturals- ;(stream-take naturals-- 10) ;---FIBONACCI--- (define fibonacci- (stream-cons 0 (stream-cons 1 (stream-zip-with + fibonacci- (stream-rest fibonacci-))))) ;fibonacci- ;(time (drop (stream-take fibonacci- 35) 34)) ;(stream-take fibonacci- 10) ;---NR PARE--- ;(define evens ;(stream-map (λ (x) (* 2 x)) naturals)) ;(stream-filter even? naturals)) ;(stream-zip-with + naturals naturals)) ;(stream-take evens 10) ;---PUTERILE LUI 2--- (define powers-of-2 (stream-cons 1 (stream-zip-with + powers-of-2 powers-of-2))) ;(stream-take powers-of-2 10) ;1 2 4 8 16 ;1 2 4 8 16 ;---------- ;2 4 8 16 32... ;---1/N!--- (define rev-facts (stream-cons 1 (stream-zip-with / rev-facts (stream-rest naturals)))) ; 1 1 1/2 1/6 ;0 1 2 3 4 ;---------------- ; 1 1/2 1/6 1/24 ;(stream-take rev-facts 10) ;---SUME PARTIALE--- (define (part-sums s) ; ineficient (stream-cons 0. (stream-zip-with + s (part-sums s)))) ;0 1 2 3 4 5 - fluxul initial ;0 0 1 3 6 10 15 - fluxul sumelor partiale ;--------------- ;0 1 3 6 10 15 ... ;(drop (stream-take (part-sums rev-facts) 2000) 1999) (define (part-sums- s) (letrec ((sums (stream-cons 0. (stream-zip-with + s sums)))) sums)) (drop (stream-take (part-sums- rev-facts) 4000) 3999) ;-----------------ERATOSTENE---------------------- (define (sieve s) (let ((p (stream-first s))) (stream-cons p (sieve (stream-filter (λ (n) (> (modulo n p) 0)) (stream-rest s)))))) (define primes (sieve (stream-rest (stream-rest naturals)))) (stream-take primes 12)