#lang racket ;---------------------- Funcții ca valori evaluate la ele însele (define a +) ;(a 2 3 5) ;null? (define fs (list + (λ (x y) (- x y x)) 5)) ;fs ;((cadr fs) 20 8) ;---------------------- Funcții ca valori de retur (define (f x) (if (< x 100) + -)) ;(f 25) ;((f 120) 16 4) (define (g x) (λ (y) ; y = '(5 2) (cons x y))) ; x = 2 ;(g 2) ;( (g 2) '(5 2) ) ;---------------------- curry / uncurry (define (plus-curry x) (λ (y) ; λ(y) (+ x y ))) ; (+ 1 y) (define (plus-uncurry x y) (+ x y)) ; identic cu (define plus-uncurry +) ;(plus-uncurry 2 4) ;(plus-uncurry 2) ;((plus-curry 2) 3) ;(plus-curry 2) (define inc-curry (plus-curry 1)) ;(inc-curry 5) (define inc-uncurry (λ (x) (plus-uncurry 1 x))) ;(inc-uncurry 5) ;(map (plus-curry 1) '(2 3 4)) ;(map (λ (x) (+ x 1)) '(2 3 4)) ;---------------------- Reutilizare de cod (define (ins-sort op) (λ (L) (if (null? L) L (insert (car L) ((ins-sort op) (cdr L)) op)))) ; wishful thinking (define (insert x L op) (if (or (null? L) (op x (car L))) (cons x L) (cons (car L) (insert x (cdr L) op)))) (define sort< (ins-sort <)) (define sort> (ins-sort >)) ;(sort< '(5 3 1 4)) ;(sort> '(5 3 1 4)) ; intoarce lista in care toate parele au fost transformate si imparele au ramas la fel ;(define (transform-evens f L) ; f = transformarea ; (cond ((null? L) L) ; ((even? (car L)) (cons (f (car L)) (transform-evens f (cdr L)))) ; (else (cons (car L) (transform-evens f (cdr L)))))) ;(transform-evens inc-curry '(1 2 3 5)) ; ;(define (transform-odds f L) ; (cond ((null? L) L) ; ((odd? (car L)) (cons (f (car L)) (transform-odds f (cdr L)))) ; (else (cons (car L) (transform-odds f (cdr L)))))) ;(transform-odds inc-curry '(1 2 3 5)) (define (transform-pred pred?) (λ (f L) (cond ((null? L) L) ((pred? (car L)) (cons (f (car L)) ((transform-pred pred?) f (cdr L)))) (else (cons (car L) ((transform-pred pred?) f (cdr L))))))) (define transform-evens (transform-pred even?)) (define transform-odds (transform-pred odd?)) ;(transform-evens inc-curry '(1 2 3 5)) ;(transform-odds inc-curry '(1 2 3 5)) ; sa sortez descrescator listele interioare ;((transform-pred list?) sort> '(1 3 (2 4) (4 5 2) 5)) ;---------------------- Șabloane comune (define (sum-series start stop f next) (if (> start stop) 0 (+ (f start) (sum-series (next start) stop f next)))) (define (sum-interval a b) (if (> a b) ; start , stop 0 (+ a (sum-interval (add1 a) b)))) ; f(start) + apel-rec(next(start)) (define (ex1 a b) (sum-series a b (λ (x) x) add1)) ;(ex1 1 10) ; e = 1/0! + 1/1! + 1/2! + 1/3! + ... (define (ex2 n) (sum-series 0 n (λ (x) (/ 1. (factorial x))) add1)) (define (sum-e n) (define (iter i) (if (> i n) 0 (+ (/ 1. (factorial i)) (iter (add1 i))))) (iter 0)) ; de implementat cu map / filter / foldl / foldr / apply ; (range x y) = (x x+1 x+2 .. y-1) (define (factorial n) (foldl * 1 (range 1 (add1 n)))) ;(map factorial (range 1 10)) ;(sum-e 15) ;(ex2 10) (define (ex3 n) (sum-series 1 n (λ (x) (/ 1. (sqr x))) (λ (x) (+ x 2)))) (define (sum-pi2/8 n) (define (iter i) (if (> i n) 0 (+ (/ 1. (sqr i)) (iter (+ 2 i))))) (iter 1)) ;(sqrt (* 8 (sum-pi2/8 111111))) ;(sqrt (* 8 (ex3 111111))) ;---------------------- Funcționale (define (max-list L) (if (null? L) -inf.0 ; acc (max (car L) (max-list (cdr L))))) ; (f (car L) apel-rec) ;(max-list '(5 2 1 4 11 5 7 5 7 3)) ;(foldl max -inf.0 '(5 2 1 4 11 5 7 5 7 3)) (define (count-odds L) (if (null? L) 0 ; acc (+ (if (odd? (car L)) 1 0) (count-odds (cdr L))))) ;(count-odds '(1 2 3)) ;(foldr (λ (x acc) (if (odd? x) (add1 acc) acc)) 0 '(1 2 3 5 7)) (define (smaller-than x L) (cond ((null? L) L) ((< (car L) x) (cons (car L) (smaller-than x (cdr L)))) (else (smaller-than x (cdr L))))) ;(smaller-than 3 '(1 2 3)) (define (smaller-than-f x L) (filter (λ (elem) (< elem x)) L)) ;(smaller-than-f 3 '(1 2 3 4 1 3 2)) (define (pronouns words) (cond ((null? words) '()) ((member (car words) '(I you he she we they)) (cons (car words) (pronouns (cdr words)))) (else (pronouns (cdr words))))) ;(filter (λ (word) (member word '(I you he she we they))) '(a I me you ha ha)) ;(pronouns ; '(In the town where I was born ; Lived a man who sailed to sea ; And he told us of his life ; In the land of submarines ; So we sailed up to the sun ; 'Til we found a sea of green ; And we lived beneath the waves ; In our yellow submarine)) (define (initials names) (if (null? names) '() (cons (substring (car names) 0 1) (initials (cdr names))))) ;(initials '("Winston" "Leonard" "Spencer" "Churchill")) ;(map (λ (name) (substring name 0 1)) '("Winston" "Leonard" "Spencer" "Churchill")) (define (squares L) (if (null? L) L (cons (sqr (car L)) (squares (cdr L))))) ;(squares '(1 2 3)) ;(map sqr '(1 2 3)) ;---------------------- TDA-uri (define make-complex cons) (define real car) (define imag cdr) (define (add-c C1 C2) (make-complex (+ (real C1) (real C2)) (+ (imag C1) (imag C2)))) ;(add-c (make-complex -1 2) (make-complex 4 1)) ;Constructori ; empty-bst : -> BST (define empty-bst '()) ; make-bst : BST x Elem x BST -> BST (define make-bst list) ;Operatori ; left : BST -> BST (define left first) ; right : BST -> BST (define right third) ; key : BST -> Elem (define key second) ; bst-empty? : BST -> Bool (define bst-empty? null?) ; insert-bst : Elem x BST -> BST (define (insert-bst x tree) (cond ((bst-empty? tree) (make-bst empty-bst x empty-bst)) ((< x (key tree)) (make-bst (insert-bst x (left tree)) (key tree) (right tree))) (else (make-bst (left tree) (key tree) (insert-bst x (right tree)))))) ; list->bst : List -> BST (define (list->bst L) (foldl insert-bst empty-bst L)) ;(list->bst '(3 6 1 2 4 8)) ;'((() 1 (() 2 ())) 3 ((() 4 ()) 6 (() 8 ()))) ; 3 ; 1 6 ; 2 4 8 ;------------------- Rezolvare test curs (define (replicate n L) (define (rep-h L acc) (if (null? L) (reverse acc) (rep-h (cdr L) (append (make-list n (car L)) acc)))) (rep-h L '())) ;(replicate 3 (range 2 10))